annotate core/modules/node/src/NodeListBuilder.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\node;
Chris@0 4
Chris@0 5 use Drupal\Core\Datetime\DateFormatterInterface;
Chris@0 6 use Drupal\Core\Entity\EntityInterface;
Chris@0 7 use Drupal\Core\Entity\EntityListBuilder;
Chris@0 8 use Drupal\Core\Entity\EntityStorageInterface;
Chris@0 9 use Drupal\Core\Entity\EntityTypeInterface;
Chris@0 10 use Drupal\Core\Language\LanguageInterface;
Chris@0 11 use Drupal\Core\Routing\RedirectDestinationInterface;
Chris@0 12 use Symfony\Component\DependencyInjection\ContainerInterface;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Defines a class to build a listing of node entities.
Chris@0 16 *
Chris@0 17 * @see \Drupal\node\Entity\Node
Chris@0 18 */
Chris@0 19 class NodeListBuilder extends EntityListBuilder {
Chris@0 20
Chris@0 21 /**
Chris@0 22 * The date formatter service.
Chris@0 23 *
Chris@0 24 * @var \Drupal\Core\Datetime\DateFormatterInterface
Chris@0 25 */
Chris@0 26 protected $dateFormatter;
Chris@0 27
Chris@0 28 /**
Chris@0 29 * The redirect destination service.
Chris@0 30 *
Chris@0 31 * @var \Drupal\Core\Routing\RedirectDestinationInterface
Chris@0 32 */
Chris@0 33 protected $redirectDestination;
Chris@0 34
Chris@0 35 /**
Chris@0 36 * Constructs a new NodeListBuilder object.
Chris@0 37 *
Chris@0 38 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 39 * The entity type definition.
Chris@0 40 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
Chris@0 41 * The entity storage class.
Chris@0 42 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
Chris@0 43 * The date formatter service.
Chris@0 44 * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
Chris@0 45 * The redirect destination service.
Chris@0 46 */
Chris@0 47 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
Chris@0 48 parent::__construct($entity_type, $storage);
Chris@0 49
Chris@0 50 $this->dateFormatter = $date_formatter;
Chris@0 51 $this->redirectDestination = $redirect_destination;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * {@inheritdoc}
Chris@0 56 */
Chris@0 57 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
Chris@0 58 return new static(
Chris@0 59 $entity_type,
Chris@0 60 $container->get('entity.manager')->getStorage($entity_type->id()),
Chris@0 61 $container->get('date.formatter'),
Chris@0 62 $container->get('redirect.destination')
Chris@0 63 );
Chris@0 64 }
Chris@0 65
Chris@0 66 /**
Chris@0 67 * {@inheritdoc}
Chris@0 68 */
Chris@0 69 public function buildHeader() {
Chris@0 70 // Enable language column and filter if multiple languages are added.
Chris@0 71 $header = [
Chris@0 72 'title' => $this->t('Title'),
Chris@0 73 'type' => [
Chris@0 74 'data' => $this->t('Content type'),
Chris@0 75 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
Chris@0 76 ],
Chris@0 77 'author' => [
Chris@0 78 'data' => $this->t('Author'),
Chris@0 79 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 80 ],
Chris@0 81 'status' => $this->t('Status'),
Chris@0 82 'changed' => [
Chris@0 83 'data' => $this->t('Updated'),
Chris@0 84 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 85 ],
Chris@0 86 ];
Chris@0 87 if (\Drupal::languageManager()->isMultilingual()) {
Chris@0 88 $header['language_name'] = [
Chris@0 89 'data' => $this->t('Language'),
Chris@0 90 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 91 ];
Chris@0 92 }
Chris@0 93 return $header + parent::buildHeader();
Chris@0 94 }
Chris@0 95
Chris@0 96 /**
Chris@0 97 * {@inheritdoc}
Chris@0 98 */
Chris@0 99 public function buildRow(EntityInterface $entity) {
Chris@0 100 /** @var \Drupal\node\NodeInterface $entity */
Chris@0 101 $mark = [
Chris@0 102 '#theme' => 'mark',
Chris@0 103 '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
Chris@0 104 ];
Chris@0 105 $langcode = $entity->language()->getId();
Chris@0 106 $uri = $entity->urlInfo();
Chris@0 107 $options = $uri->getOptions();
Chris@0 108 $options += ($langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : []);
Chris@0 109 $uri->setOptions($options);
Chris@0 110 $row['title']['data'] = [
Chris@0 111 '#type' => 'link',
Chris@0 112 '#title' => $entity->label(),
Chris@0 113 '#suffix' => ' ' . \Drupal::service('renderer')->render($mark),
Chris@0 114 '#url' => $uri,
Chris@0 115 ];
Chris@0 116 $row['type'] = node_get_type_label($entity);
Chris@0 117 $row['author']['data'] = [
Chris@0 118 '#theme' => 'username',
Chris@0 119 '#account' => $entity->getOwner(),
Chris@0 120 ];
Chris@0 121 $row['status'] = $entity->isPublished() ? $this->t('published') : $this->t('not published');
Chris@0 122 $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
Chris@0 123 $language_manager = \Drupal::languageManager();
Chris@0 124 if ($language_manager->isMultilingual()) {
Chris@0 125 $row['language_name'] = $language_manager->getLanguageName($langcode);
Chris@0 126 }
Chris@0 127 $row['operations']['data'] = $this->buildOperations($entity);
Chris@0 128 return $row + parent::buildRow($entity);
Chris@0 129 }
Chris@0 130
Chris@0 131 /**
Chris@0 132 * {@inheritdoc}
Chris@0 133 */
Chris@0 134 protected function getDefaultOperations(EntityInterface $entity) {
Chris@0 135 $operations = parent::getDefaultOperations($entity);
Chris@0 136
Chris@0 137 $destination = $this->redirectDestination->getAsArray();
Chris@0 138 foreach ($operations as $key => $operation) {
Chris@0 139 $operations[$key]['query'] = $destination;
Chris@0 140 }
Chris@0 141 return $operations;
Chris@0 142 }
Chris@0 143
Chris@0 144 }