ModeratedNodeListBuilder.php in Drupal 8
File
core/modules/content_moderation/src/ModeratedNodeListBuilder.php
View source
<?php
namespace Drupal\content_moderation;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RedirectDestinationInterface;
use Drupal\node\NodeListBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ModeratedNodeListBuilder extends NodeListBuilder {
protected $storage;
protected $entityTypeManager;
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($entity_type, $storage, $date_formatter, $redirect_destination);
$this->entityTypeManager = $entity_type_manager;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
$entity_type_manager = $container
->get('entity_type.manager');
return new static($entity_type, $entity_type_manager
->getStorage($entity_type
->id()), $container
->get('date.formatter'), $container
->get('redirect.destination'), $entity_type_manager);
}
public function load() {
$revision_ids = $this
->getEntityRevisionIds();
return $this->storage
->loadMultipleRevisions($revision_ids);
}
protected function getEntityRevisionIds() {
$query = $this->entityTypeManager
->getStorage('content_moderation_state')
->getAggregateQuery()
->aggregate('content_entity_id', 'MAX')
->groupBy('content_entity_revision_id')
->condition('content_entity_type_id', $this->entityTypeId)
->condition('moderation_state', 'published', '<>')
->sort('content_entity_revision_id', 'DESC');
if ($this->limit) {
$query
->pager($this->limit);
}
$result = $query
->execute();
return $result ? array_column($result, 'content_entity_revision_id') : [];
}
public function buildHeader() {
$header = parent::buildHeader();
$header['status'] = $this
->t('Moderation state');
return $header;
}
public function buildRow(EntityInterface $entity) {
$row = parent::buildRow($entity);
$row['status'] = $entity->moderation_state->value;
return $row;
}
public function render() {
$build = parent::render();
$build['table']['#empty'] = $this
->t('There is no moderated @label yet. Only pending versions of @label, such as drafts, are listed here.', [
'@label' => $this->entityType
->getLabel(),
]);
return $build;
}
}