NodeListBuilder.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Drupal\node;
  3. use Drupal\Core\Datetime\DateFormatterInterface;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Entity\EntityListBuilder;
  6. use Drupal\Core\Entity\EntityStorageInterface;
  7. use Drupal\Core\Entity\EntityTypeInterface;
  8. use Drupal\Core\Language\LanguageInterface;
  9. use Drupal\Core\Routing\RedirectDestinationInterface;
  10. use Symfony\Component\DependencyInjection\ContainerInterface;
  11. /**
  12. * Defines a class to build a listing of node entities.
  13. *
  14. * @see \Drupal\node\Entity\Node
  15. */
  16. class NodeListBuilder extends EntityListBuilder {
  17. /**
  18. * The date formatter service.
  19. *
  20. * @var \Drupal\Core\Datetime\DateFormatterInterface
  21. */
  22. protected $dateFormatter;
  23. /**
  24. * Constructs a new NodeListBuilder object.
  25. *
  26. * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
  27. * The entity type definition.
  28. * @param \Drupal\Core\Entity\EntityStorageInterface $storage
  29. * The entity storage class.
  30. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
  31. * The date formatter service.
  32. * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
  33. * The redirect destination service.
  34. */
  35. public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
  36. parent::__construct($entity_type, $storage);
  37. $this->dateFormatter = $date_formatter;
  38. $this->redirectDestination = $redirect_destination;
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
  44. return new static(
  45. $entity_type,
  46. $container->get('entity.manager')->getStorage($entity_type->id()),
  47. $container->get('date.formatter'),
  48. $container->get('redirect.destination')
  49. );
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function buildHeader() {
  55. // Enable language column and filter if multiple languages are added.
  56. $header = [
  57. 'title' => $this->t('Title'),
  58. 'type' => [
  59. 'data' => $this->t('Content type'),
  60. 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
  61. ],
  62. 'author' => [
  63. 'data' => $this->t('Author'),
  64. 'class' => [RESPONSIVE_PRIORITY_LOW],
  65. ],
  66. 'status' => $this->t('Status'),
  67. 'changed' => [
  68. 'data' => $this->t('Updated'),
  69. 'class' => [RESPONSIVE_PRIORITY_LOW],
  70. ],
  71. ];
  72. if (\Drupal::languageManager()->isMultilingual()) {
  73. $header['language_name'] = [
  74. 'data' => $this->t('Language'),
  75. 'class' => [RESPONSIVE_PRIORITY_LOW],
  76. ];
  77. }
  78. return $header + parent::buildHeader();
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function buildRow(EntityInterface $entity) {
  84. /** @var \Drupal\node\NodeInterface $entity */
  85. $mark = [
  86. '#theme' => 'mark',
  87. '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
  88. ];
  89. $langcode = $entity->language()->getId();
  90. $uri = $entity->urlInfo();
  91. $options = $uri->getOptions();
  92. $options += ($langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : []);
  93. $uri->setOptions($options);
  94. $row['title']['data'] = [
  95. '#type' => 'link',
  96. '#title' => $entity->label(),
  97. '#suffix' => ' ' . \Drupal::service('renderer')->render($mark),
  98. '#url' => $uri,
  99. ];
  100. $row['type'] = node_get_type_label($entity);
  101. $row['author']['data'] = [
  102. '#theme' => 'username',
  103. '#account' => $entity->getOwner(),
  104. ];
  105. $row['status'] = $entity->isPublished() ? $this->t('published') : $this->t('not published');
  106. $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
  107. $language_manager = \Drupal::languageManager();
  108. if ($language_manager->isMultilingual()) {
  109. $row['language_name'] = $language_manager->getLanguageName($langcode);
  110. }
  111. $row['operations']['data'] = $this->buildOperations($entity);
  112. return $row + parent::buildRow($entity);
  113. }
  114. }