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