62666497e877b55c73602eba554cee3eb66eda0f
[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    * The redirect destination service.
30    *
31    * @var \Drupal\Core\Routing\RedirectDestinationInterface
32    */
33   protected $redirectDestination;
34
35   /**
36    * Constructs a new NodeListBuilder object.
37    *
38    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
39    *   The entity type definition.
40    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
41    *   The entity storage class.
42    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
43    *   The date formatter service.
44    * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
45    *   The redirect destination service.
46    */
47   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
48     parent::__construct($entity_type, $storage);
49
50     $this->dateFormatter = $date_formatter;
51     $this->redirectDestination = $redirect_destination;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
58     return new static(
59       $entity_type,
60       $container->get('entity.manager')->getStorage($entity_type->id()),
61       $container->get('date.formatter'),
62       $container->get('redirect.destination')
63     );
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function buildHeader() {
70     // Enable language column and filter if multiple languages are added.
71     $header = [
72       'title' => $this->t('Title'),
73       'type' => [
74         'data' => $this->t('Content type'),
75         'class' => [RESPONSIVE_PRIORITY_MEDIUM],
76       ],
77       'author' => [
78         'data' => $this->t('Author'),
79         'class' => [RESPONSIVE_PRIORITY_LOW],
80       ],
81       'status' => $this->t('Status'),
82       'changed' => [
83         'data' => $this->t('Updated'),
84         'class' => [RESPONSIVE_PRIORITY_LOW],
85       ],
86     ];
87     if (\Drupal::languageManager()->isMultilingual()) {
88       $header['language_name'] = [
89         'data' => $this->t('Language'),
90         'class' => [RESPONSIVE_PRIORITY_LOW],
91       ];
92     }
93     return $header + parent::buildHeader();
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function buildRow(EntityInterface $entity) {
100     /** @var \Drupal\node\NodeInterface $entity */
101     $mark = [
102       '#theme' => 'mark',
103       '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
104     ];
105     $langcode = $entity->language()->getId();
106     $uri = $entity->urlInfo();
107     $options = $uri->getOptions();
108     $options += ($langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : []);
109     $uri->setOptions($options);
110     $row['title']['data'] = [
111       '#type' => 'link',
112       '#title' => $entity->label(),
113       '#suffix' => ' ' . drupal_render($mark),
114       '#url' => $uri,
115     ];
116     $row['type'] = node_get_type_label($entity);
117     $row['author']['data'] = [
118       '#theme' => 'username',
119       '#account' => $entity->getOwner(),
120     ];
121     $row['status'] = $entity->isPublished() ? $this->t('published') : $this->t('not published');
122     $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
123     $language_manager = \Drupal::languageManager();
124     if ($language_manager->isMultilingual()) {
125       $row['language_name'] = $language_manager->getLanguageName($langcode);
126     }
127     $row['operations']['data'] = $this->buildOperations($entity);
128     return $row + parent::buildRow($entity);
129   }
130
131   /**
132    * {@inheritdoc}
133    */
134   protected function getDefaultOperations(EntityInterface $entity) {
135     $operations = parent::getDefaultOperations($entity);
136
137     $destination = $this->redirectDestination->getAsArray();
138     foreach ($operations as $key => $operation) {
139       $operations[$key]['query'] = $destination;
140     }
141     return $operations;
142   }
143
144 }