annotate core/modules/node/src/NodeListBuilder.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
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 * Constructs a new NodeListBuilder object.
Chris@0 30 *
Chris@0 31 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
Chris@0 32 * The entity type definition.
Chris@0 33 * @param \Drupal\Core\Entity\EntityStorageInterface $storage
Chris@0 34 * The entity storage class.
Chris@0 35 * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
Chris@0 36 * The date formatter service.
Chris@0 37 * @param \Drupal\Core\Routing\RedirectDestinationInterface $redirect_destination
Chris@0 38 * The redirect destination service.
Chris@0 39 */
Chris@0 40 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter, RedirectDestinationInterface $redirect_destination) {
Chris@0 41 parent::__construct($entity_type, $storage);
Chris@0 42
Chris@0 43 $this->dateFormatter = $date_formatter;
Chris@0 44 $this->redirectDestination = $redirect_destination;
Chris@0 45 }
Chris@0 46
Chris@0 47 /**
Chris@0 48 * {@inheritdoc}
Chris@0 49 */
Chris@0 50 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
Chris@0 51 return new static(
Chris@0 52 $entity_type,
Chris@18 53 $container->get('entity_type.manager')->getStorage($entity_type->id()),
Chris@0 54 $container->get('date.formatter'),
Chris@0 55 $container->get('redirect.destination')
Chris@0 56 );
Chris@0 57 }
Chris@0 58
Chris@0 59 /**
Chris@0 60 * {@inheritdoc}
Chris@0 61 */
Chris@0 62 public function buildHeader() {
Chris@0 63 // Enable language column and filter if multiple languages are added.
Chris@0 64 $header = [
Chris@0 65 'title' => $this->t('Title'),
Chris@0 66 'type' => [
Chris@0 67 'data' => $this->t('Content type'),
Chris@0 68 'class' => [RESPONSIVE_PRIORITY_MEDIUM],
Chris@0 69 ],
Chris@0 70 'author' => [
Chris@0 71 'data' => $this->t('Author'),
Chris@0 72 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 73 ],
Chris@0 74 'status' => $this->t('Status'),
Chris@0 75 'changed' => [
Chris@0 76 'data' => $this->t('Updated'),
Chris@0 77 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 78 ],
Chris@0 79 ];
Chris@0 80 if (\Drupal::languageManager()->isMultilingual()) {
Chris@0 81 $header['language_name'] = [
Chris@0 82 'data' => $this->t('Language'),
Chris@0 83 'class' => [RESPONSIVE_PRIORITY_LOW],
Chris@0 84 ];
Chris@0 85 }
Chris@0 86 return $header + parent::buildHeader();
Chris@0 87 }
Chris@0 88
Chris@0 89 /**
Chris@0 90 * {@inheritdoc}
Chris@0 91 */
Chris@0 92 public function buildRow(EntityInterface $entity) {
Chris@0 93 /** @var \Drupal\node\NodeInterface $entity */
Chris@0 94 $mark = [
Chris@0 95 '#theme' => 'mark',
Chris@0 96 '#mark_type' => node_mark($entity->id(), $entity->getChangedTime()),
Chris@0 97 ];
Chris@0 98 $langcode = $entity->language()->getId();
Chris@18 99 $uri = $entity->toUrl();
Chris@0 100 $options = $uri->getOptions();
Chris@0 101 $options += ($langcode != LanguageInterface::LANGCODE_NOT_SPECIFIED && isset($languages[$langcode]) ? ['language' => $languages[$langcode]] : []);
Chris@0 102 $uri->setOptions($options);
Chris@0 103 $row['title']['data'] = [
Chris@0 104 '#type' => 'link',
Chris@0 105 '#title' => $entity->label(),
Chris@0 106 '#suffix' => ' ' . \Drupal::service('renderer')->render($mark),
Chris@0 107 '#url' => $uri,
Chris@0 108 ];
Chris@0 109 $row['type'] = node_get_type_label($entity);
Chris@0 110 $row['author']['data'] = [
Chris@0 111 '#theme' => 'username',
Chris@0 112 '#account' => $entity->getOwner(),
Chris@0 113 ];
Chris@0 114 $row['status'] = $entity->isPublished() ? $this->t('published') : $this->t('not published');
Chris@0 115 $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
Chris@0 116 $language_manager = \Drupal::languageManager();
Chris@0 117 if ($language_manager->isMultilingual()) {
Chris@0 118 $row['language_name'] = $language_manager->getLanguageName($langcode);
Chris@0 119 }
Chris@0 120 $row['operations']['data'] = $this->buildOperations($entity);
Chris@0 121 return $row + parent::buildRow($entity);
Chris@0 122 }
Chris@0 123
Chris@0 124 }