NodeListBuilder.php 3.71 KB
Newer Older
1
2
3
4
<?php

namespace Drupal\node;

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\Routing\RedirectDestinationInterface;
11
12
13
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
14
15
16
 * Defines a class to build a listing of node entities.
 *
 * @see \Drupal\node\Entity\Node
17
 */
18
class NodeListBuilder extends EntityListBuilder {
19
20

  /**
21
   * The date formatter service.
22
   *
23
   * @var \Drupal\Core\Datetime\DateFormatterInterface
24
   */
25
  protected $dateFormatter;
26
27

  /**
28
   * Constructs a new NodeListBuilder object.
29
   *
30
31
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
32
33
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
34
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
35
   *   The date formatter service.
36
37
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   The redirect destination service.
38
   */
39
  public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
40
    parent::__construct($entity_type, $storage);
41

42
    $this->dateFormatter = $date_formatter;
43
    $this->redirectDestination = $redirect_destination;
44
45
46
47
48
  }

  /**
   * {@inheritdoc}
   */
49
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
50
    return new static(
51
      $entity_type,
52
      $container->get('entity_type.manager')->getStorage($entity_type->id()),
53
54
      $container->get('date.formatter'),
      $container->get('redirect.destination')
55
56
57
58
59
60
61
    );
  }

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
62
    // Enable language column and filter if multiple languages are added.
63
    $header = [
64
      'title' => $this->t('Title'),
65
      'type' => [
66
        'data' => $this->t('Content type'),
67
68
69
        'class' => [RESPONSIVE_PRIORITY_MEDIUM],
      ],
      'author' => [
70
        'data' => $this->t('Author'),
71
72
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ],
73
      'status' => $this->t('Status'),
74
      'changed' => [
75
        'data' => $this->t('Updated'),
76
77
78
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ],
    ];
79
    if (\Drupal::languageManager()->isMultilingual()) {
80
      $header['language_name'] = [
81
        'data' => $this->t('Language'),
82
83
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ];
84
85
86
87
88
89
90
91
    }
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
92
    /** @var \Drupal\node\NodeInterface $entity */
93
    $mark = [
94
95
      '#theme' => 'mark',
      '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
96
97
    ];
    $row['title']['data'] = [
98
99
      '#type' => 'link',
      '#title' => $entity->label(),
100
      '#suffix' => ' ' . \Drupal::service('renderer')->render($mark),
101
      '#url' => $entity->toUrl(),
102
    ];
103
    $row['type'] = node_get_type_label($entity);
104
    $row['author']['data'] = [
105
      '#theme' => 'username',
106
      '#account' => $entity->getOwner(),
107
    ];
108
    $row['status'] = $entity->isPublished() ? $this->t('published') : $this->t('not published');
109
    $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
110
111
    $language_manager = \Drupal::languageManager();
    if ($language_manager->isMultilingual()) {
112
      $row['language_name'] = $language_manager->getLanguageName($entity->language()->getId());
113
114
115
116
117
118
    }
    $row['operations']['data'] = $this->buildOperations($entity);
    return $row + parent::buildRow($entity);
  }

}