NodeListBuilder.php 4.57 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\Language\LanguageInterface;
11
use Drupal\Core\Routing\RedirectDestinationInterface;
12
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
15
16
17
 * Defines a class to build a listing of node entities.
 *
 * @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
30
31
32
33
34
  /**
   * The redirect destination service.
   *
   * @var \Drupal\Core\Routing\RedirectDestinationInterface
   */
  protected $redirectDestination;

35
  /**
36
   * Constructs a new NodeListBuilder object.
37
   *
38
39
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
40
41
   * @param \Drupal\Core\Entity\EntityStorageInterface $storage
   *   The entity storage class.
42
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
43
   *   The date formatter service.
44
45
   * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
   *   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
56
  }

  /**
   * {@inheritdoc}
   */
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
62
      $container->get('date.formatter'),
      $container->get('redirect.destination')
63
64
65
66
67
68
69
    );
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
100
    /** @var \Drupal\node\NodeInterface $entity */
101
102
103
104
    $mark = array(
      '#theme' => 'mark',
      '#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]) ? array('language' => $languages[$langcode]) : array());
109
    $uri->setOptions($options);
110
111
112
    $row['title']['data'] = array(
      '#type' => 'link',
      '#title' => $entity->label(),
113
      '#suffix' => ' ' . \Drupal::service('renderer')->render($mark),
114
115
      '#url' => $uri,
    );
116
    $row['type'] = node_get_type_label($entity);
117
118
    $row['author']['data'] = array(
      '#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
124
125
    $language_manager = \Drupal::languageManager();
    if ($language_manager->isMultilingual()) {
      $row['language_name'] = $language_manager->getLanguageName($langcode);
126
127
128
129
130
131
132
133
    }
    $row['operations']['data'] = $this->buildOperations($entity);
    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
134
135
  protected function getDefaultOperations(EntityInterface $entity) {
    $operations = parent::getDefaultOperations($entity);
136

137
    $destination = $this->redirectDestination->getAsArray();
138
139
140
141
142
143
144
    foreach ($operations as $key => $operation) {
      $operations[$key]['query'] = $destination;
    }
    return $operations;
  }

}