ModeratedNodeListBuilder.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Drupal\content_moderation;
  3. use Drupal\Core\Datetime\DateFormatterInterface;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityStorageInterface;
  6. use Drupal\Core\Entity\EntityTypeInterface;
  7. use Drupal\Core\Entity\EntityTypeManagerInterface;
  8. use Drupal\Core\Routing\RedirectDestinationInterface;
  9. use Drupal\node\NodeListBuilder;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. /**
  12. * Defines a class to build a listing of moderated node entities.
  13. */
  14. class ModeratedNodeListBuilder extends NodeListBuilder {
  15. /**
  16. * The entity storage class.
  17. *
  18. * @var \Drupal\Core\Entity\RevisionableStorageInterface
  19. */
  20. protected $storage;
  21. /**
  22. * The entity type manager.
  23. *
  24. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  25. */
  26. protected $entityTypeManager;
  27. /**
  28. * Constructs a new ModeratedNodeListBuilder object.
  29. *
  30. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  31. * The entity type definition.
  32. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  33. * The entity storage class.
  34. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
  35. * The date formatter service.
  36. * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
  37. * The redirect destination service.
  38. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  39. * The entity type manager.
  40. */
  41. public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination, EntityTypeManagerInterface $entity_type_manager) {
  42. parent::__construct($entity_type, $storage, $date_formatter, $redirect_destination);
  43. $this->entityTypeManager = $entity_type_manager;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  49. return new static(
  50. $entity_type,
  51. $container->get('entity.manager')->getStorage($entity_type->id()),
  52. $container->get('date.formatter'),
  53. $container->get('redirect.destination'),
  54. $container->get('entity_type.manager')
  55. );
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function load() {
  61. $revision_ids = $this->getEntityRevisionIds();
  62. return $this->storage->loadMultipleRevisions($revision_ids);
  63. }
  64. /**
  65. * Loads entity revision IDs using a pager sorted by the entity revision ID.
  66. *
  67. * @return array
  68. * An array of entity revision IDs.
  69. */
  70. protected function getEntityRevisionIds() {
  71. $query = $this->entityTypeManager->getStorage('content_moderation_state')->getAggregateQuery()
  72. ->aggregate('content_entity_id', 'MAX')
  73. ->groupBy('content_entity_revision_id')
  74. ->condition('content_entity_type_id', $this->entityTypeId)
  75. ->condition('moderation_state', 'published', '<>')
  76. ->sort('content_entity_revision_id', 'DESC');
  77. // Only add the pager if a limit is specified.
  78. if ($this->limit) {
  79. $query->pager($this->limit);
  80. }
  81. $result = $query->execute();
  82. return $result ? array_column($result, 'content_entity_revision_id') : [];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function buildHeader() {
  88. $header = parent::buildHeader();
  89. $header['status'] = $this->t('Moderation state');
  90. return $header;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public function buildRow(EntityInterface $entity) {
  96. $row = parent::buildRow($entity);
  97. $row['status'] = $entity->moderation_state->value;
  98. return $row;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function render() {
  104. $build = parent::render();
  105. $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()]);
  106. return $build;
  107. }
  108. }