NodeListBuilder.php 4.5 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
    $header = [
72
      'title' => $this->t('Title'),
73
      'type' => [
74
        'data' => $this->t('Content type'),
75 76 77
        'class' => [RESPONSIVE_PRIORITY_MEDIUM],
      ],
      'author' => [
78
        'data' => $this->t('Author'),
79 80
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ],
81
      'status' => $this->t('Status'),
82
      'changed' => [
83
        'data' => $this->t('Updated'),
84 85 86
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ],
    ];
87
    if (\Drupal::languageManager()->isMultilingual()) {
88
      $header['language_name'] = [
89
        'data' => $this->t('Language'),
90 91
        'class' => [RESPONSIVE_PRIORITY_LOW],
      ];
92 93 94 95 96 97 98 99
    }
    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
100
    /** @var \Drupal\node\NodeInterface $entity */
101
    $mark = [
102 103
      '#theme' => 'mark',
      '#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 112
      '#type' => 'link',
      '#title' => $entity->label(),
113
      '#suffix' => ' ' . \Drupal::service('renderer')->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 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;
  }

}