CommentSelection.php in Comment Permissions 8
File
src/Plugin/EntityReferenceSelection/CommentSelection.php
View source
<?php
namespace Drupal\comment_perm\Plugin\EntityReferenceSelection;
use Drupal\comment\CommentInterface;
use Drupal\comment\Plugin\EntityReferenceSelection\CommentSelection as CommentSelectionBase;
use Drupal\comment_perm\CommentAccessTrait;
class CommentSelection extends CommentSelectionBase {
use CommentAccessTrait;
protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
$query = parent::buildEntityQuery($match, $match_operator);
if ($this
->userHasAdminPerm()) {
$query
->addTag('comment_perm_access');
}
else {
$query
->condition('status', CommentInterface::PUBLISHED);
}
return $query;
}
public function validateReferenceableNewEntities(array $entities) {
$entities = array_filter($entities, function ($entity) {
$target_bundles = $this
->getConfiguration()['target_bundles'];
if (isset($target_bundles)) {
return in_array($entity
->bundle(), $target_bundles);
}
return TRUE;
});
if (!$this
->userHasAdminPerm()) {
$entities = array_filter($entities, function ($comment) {
return $comment
->isPublished();
});
}
return $entities;
}
protected function userHasAdminPerm() {
$is_admin = FALSE;
foreach ($this
->getConfiguration()['target_bundles'] as $comment_type) {
if ($this
->accessAdministerComment($this->currentUser, $comment_type)) {
$is_admin = TRUE;
}
}
return $is_admin;
}
}