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;
15 * Defines a class to build a listing of node entities.
17 * @see \Drupal\node\Entity\Node
19 class NodeListBuilder extends EntityListBuilder {
22 * The date formatter service.
24 * @var \Drupal\Core\Datetime\DateFormatterInterface
26 protected $dateFormatter;
29 * The redirect destination service.
31 * @var \Drupal\Core\Routing\RedirectDestinationInterface
33 protected $redirectDestination;
36 * Constructs a new NodeListBuilder object.
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.
47 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
48 parent::__construct($entity_type, $storage);
50 $this->dateFormatter = $date_formatter;
51 $this->redirectDestination = $redirect_destination;
57 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
60 $container->get('entity.manager')->getStorage($entity_type->id()),
61 $container->get('date.formatter'),
62 $container->get('redirect.destination')
69 public function buildHeader() {
70 // Enable language column and filter if multiple languages are added.
72 'title' => $this->t('Title'),
74 'data' => $this->t('Content type'),
75 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
78 'data' => $this->t('Author'),
79 'class' => [RESPONSIVE_PRIORITY_LOW],
81 'status' => $this->t('Status'),
83 'data' => $this->t('Updated'),
84 'class' => [RESPONSIVE_PRIORITY_LOW],
87 if (\Drupal::languageManager()->isMultilingual()) {
88 $header['language_name'] = [
89 'data' => $this->t('Language'),
90 'class' => [RESPONSIVE_PRIORITY_LOW],
93 return $header + parent::buildHeader();
99 public function buildRow(EntityInterface $entity) {
100 /** @var \Drupal\node\NodeInterface $entity */
103 '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
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'] = [
112 '#title' => $entity->label(),
113 '#suffix' => ' ' . drupal_render($mark),
116 $row['type'] = node_get_type_label($entity);
117 $row['author']['data'] = [
118 '#theme' => 'username',
119 '#account' => $entity->getOwner(),
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);
127 $row['operations']['data'] = $this->buildOperations($entity);
128 return $row + parent::buildRow($entity);
134 protected function getDefaultOperations(EntityInterface $entity) {
135 $operations = parent::getDefaultOperations($entity);
137 $destination = $this->redirectDestination->getAsArray();
138 foreach ($operations as $key => $operation) {
139 $operations[$key]['query'] = $destination;