1 EntityReferenceSelectionHandlerGeneric.inc public EntityReferenceSelectionHandlerGeneric_comment::entityFieldQueryAlter(SelectQueryInterface $query)

Implements EntityReferenceHandler::entityFieldQueryAlter().

Overrides EntityReferenceSelectionHandlerGeneric::entityFieldQueryAlter

File

core/modules/entityreference/plugins/selection/EntityReferenceSelectionHandlerGeneric.inc, line 449
Generic Entity handler.

Class

EntityReferenceSelectionHandlerGeneric_comment
Override for the Comment entity type.

Code

public function entityFieldQueryAlter(SelectQueryInterface $query) {
  // Adding the 'comment_access' tag is insufficient for comments: need to
  // also know about the concept of 'published' and 'unpublished'.
  if (!user_access('administer comments')) {
    $base_table = $this->ensureBaseTable($query);
    $query->condition("$base_table.status", COMMENT_PUBLISHED);
  }

  // Need to make sure that comments cannot be viewed when the user doesn't
  // have access to the node.
  $tables = $query->getTables();
  $base_table = key($tables);
  $node_alias = $query->innerJoin('node', 'n', '%alias.nid = ' . $base_table . '.nid');
  // Pass the query to the node access control.
  $this->reAlterQuery($query, 'node_access', $node_alias);

  // The comment entity exposes a bundle, but doesn't have a bundle column
  // in the database. Alter the query to fetch the bundle.
  $conditions = &$query->conditions();
  foreach ($conditions as $key => &$condition) {
    if ($key !== '#conjunction' && is_string($condition['field']) && $condition['field'] === $base_table . '.node_type') {
      $condition['field'] = '`' . $node_alias . '`.`type`';
      foreach ($condition['value'] as &$value) {
        if (substr($value, 0, 13) == 'comment_node_') {
          $value = substr($value, 13);
        }
      }
      break;
    }
  }

  // @see EntityReferenceHandler_node::entityFieldQueryAlter()
  if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
    $query->condition($node_alias . '.status', 1);
  }
}