CommentSelection.php in Zircon Profile 8
File
core/modules/comment/src/Plugin/EntityReferenceSelection/CommentSelection.php
View source
<?php
namespace Drupal\comment\Plugin\EntityReferenceSelection;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection;
use Drupal\comment\CommentInterface;
class CommentSelection extends DefaultSelection {
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
if (!$this->currentUser
->hasPermission('administer comments')) {
$query
->condition('status', CommentInterface::PUBLISHED);
}
return $query;
}
public function createNewEntity($entity_type_id, $bundle, $label, $uid) {
$comment = parent::createNewEntity($entity_type_id, $bundle, $label, $uid);
$comment
->setPublished(TRUE);
return $comment;
}
public function validateReferenceableNewEntities(array $entities) {
$entities = parent::validateReferenceableNewEntities($entities);
if (!$this->currentUser
->hasPermission('administer comments')) {
$entities = array_filter($entities, function ($comment) {
return $comment
->isPublished();
});
}
return $entities;
}
public function entityQueryAlter(SelectInterface $query) {
$tables = $query
->getTables();
$data_table = 'comment_field_data';
if (!isset($tables['comment_field_data']['alias'])) {
$query
->innerJoin($data_table, NULL, "base_table.cid = {$data_table}.cid AND {$data_table}.default_langcode = 1");
}
$node_alias = $query
->innerJoin('node_field_data', 'n', '%alias.nid = ' . $data_table . '.entity_id AND ' . $data_table . ".entity_type = 'node'");
$this
->reAlterQuery($query, 'node_access', $node_alias);
if (!$this->currentUser
->hasPermission('bypass node access') && !count($this->moduleHandler
->getImplementations('node_grants'))) {
$query
->condition($node_alias . '.status', 1);
}
}
}