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 * Constructs a new NodeListBuilder object.
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.
40 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
41 parent::__construct($entity_type, $storage);
43 $this->dateFormatter = $date_formatter;
44 $this->redirectDestination = $redirect_destination;
50 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
53 $container->get('entity.manager')->getStorage($entity_type->id()),
54 $container->get('date.formatter'),
55 $container->get('redirect.destination')
62 public function buildHeader() {
63 // Enable language column and filter if multiple languages are added.
65 'title' => $this->t('Title'),
67 'data' => $this->t('Content type'),
68 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
71 'data' => $this->t('Author'),
72 'class' => [RESPONSIVE_PRIORITY_LOW],
74 'status' => $this->t('Status'),
76 'data' => $this->t('Updated'),
77 'class' => [RESPONSIVE_PRIORITY_LOW],
80 if (\Drupal::languageManager()->isMultilingual()) {
81 $header['language_name'] = [
82 'data' => $this->t('Language'),
83 'class' => [RESPONSIVE_PRIORITY_LOW],
86 return $header + parent::buildHeader();
92 public function buildRow(EntityInterface $entity) {
93 /** @var \Drupal\node\NodeInterface $entity */
96 '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
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'] = [
105 '#title' => $entity->label(),
106 '#suffix' => ' ' . \Drupal::service('renderer')->render($mark),
109 $row['type'] = node_get_type_label($entity);
110 $row['author']['data'] = [
111 '#theme' => 'username',
112 '#account' => $entity->getOwner(),
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);
120 $row['operations']['data'] = $this->buildOperations($entity);
121 return $row + parent::buildRow($entity);