UnpublishedStatus.php in Unpublished Node Permissions 8
File
src/Plugin/views/filter/UnpublishedStatus.php
View source
<?php
namespace Drupal\unpublished_node_permissions\Plugin\views\filter;
use Drupal\node\Entity\NodeType;
use Drupal\node\Plugin\views\filter\Status;
class UnpublishedStatus extends Status {
public function query() {
$table = $this
->ensureMyTable();
$account = \Drupal::currentUser();
$query_addition = [];
foreach (NodeType::loadMultiple() as $type) {
$type_id = $type
->id();
if ($account
->hasPermission("view {$type_id} unpublished content")) {
$query_addition[] = "{$table}.type = '{$type_id}'";
}
}
$query = "{$table}.status = 1 OR ({$table}.uid = ***CURRENT_USER*** AND ***CURRENT_USER*** <> 0 AND ***VIEW_OWN_UNPUBLISHED_NODES*** = 1) OR ***BYPASS_NODE_ACCESS*** = 1";
if (count($query_addition)) {
$query .= ' OR ';
$query .= '(' . implode(' OR ', $query_addition) . ')';
}
$this->query
->addWhereExpression($this->options['group'], $query);
}
public function getCacheContexts() {
$contexts = parent::getCacheContexts();
$contexts[] = 'user.roles';
return $contexts;
}
}