class CommentSelection

Provides specific access control for the comment entity type.

Plugin annotation


@Plugin(
  id = "comment_default",
  module = "comment",
  label = @Translation("Comment selection"),
  entity_types = {"comment"},
  group = "default",
  weight = 1
)

Hierarchy

Expanded class hierarchy of CommentSelection

File

drupal/core/modules/comment/lib/Drupal/comment/Plugin/entity_reference/selection/CommentSelection.php, line 27
Contains \Drupal\comment\Plugin\entity_reference\selection\CommentSelection.

Namespace

Drupal\comment\Plugin\entity_reference\selection
View source
class CommentSelection extends SelectionBase {

  /**
   * Overrides SelectionBase::buildEntityQuery().
   */
  public function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
    $query = parent::buildEntityQuery($match, $match_operator);

    // Adding the 'comment_access' tag is sadly insufficient for comments:
    // core requires us to also know about the concept of 'published' and
    // 'unpublished'.
    if (!user_access('administer comments')) {
      $query
        ->condition('status', COMMENT_PUBLISHED);
    }
    return $query;
  }

  /**
   * Overrides SelectionBase::entityQueryAlter().
   */
  public function entityQueryAlter(SelectInterface $query) {
    $tables = $query
      ->getTables();
    $base_table = $tables['base_table']['alias'];

    // The Comment module doesn't implement any proper comment access,
    // and as a consequence doesn't make sure that comments cannot be viewed
    // when the user doesn't have access to the node.
    $node_alias = $query
      ->innerJoin('node_field_data', 'n', '%alias.nid = ' . $base_table . '.nid');

    // Pass the query to the node access control.
    $this
      ->reAlterQuery($query, 'node_access', $node_alias);

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

    // Passing the query to node_query_node_access_alter() is sadly
    // insufficient for nodes.
    // @see SelectionEntityTypeNode::entityQueryAlter()
    if (!user_access('bypass node access') && !count(module_implements('node_grants'))) {
      $query
        ->condition($node_alias . '.status', 1);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CommentSelection::buildEntityQuery public function Overrides SelectionBase::buildEntityQuery(). Overrides SelectionBase::buildEntityQuery
CommentSelection::entityQueryAlter public function Overrides SelectionBase::entityQueryAlter(). Overrides SelectionBase::entityQueryAlter
SelectionBase::$entity protected property The entity object, or NULL
SelectionBase::$field protected property The field array.
SelectionBase::$instance protected property The instance array.
SelectionBase::countReferencableEntities public function Implements SelectionInterface::countReferencableEntities(). Overrides SelectionInterface::countReferencableEntities
SelectionBase::getReferencableEntities public function Implements SelectionInterface::getReferencableEntities(). Overrides SelectionInterface::getReferencableEntities 1
SelectionBase::reAlterQuery protected function Helper method: Passes a query to the alteration system again.
SelectionBase::settingsForm public static function Implements SelectionInterface::settingsForm(). Overrides SelectionInterface::settingsForm 2
SelectionBase::validateAutocompleteInput public function Implements SelectionInterface::validateAutocompleteInput(). Overrides SelectionInterface::validateAutocompleteInput
SelectionBase::validateReferencableEntities public function Implements SelectionInterface::validateReferencableEntities(). Overrides SelectionInterface::validateReferencableEntities
SelectionBase::__construct public function Constructs a SelectionBase object.