Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / SynonymListBuilder.php
1 <?php
2
3 namespace Drupal\search_api_synonym;
4
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\Link;
12 use Drupal\Core\Url;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
14
15 /**
16  * Defines a class to build a listing of Synonym entities.
17  *
18  * @ingroup search_api_synonym
19  */
20 class SynonymListBuilder extends EntityListBuilder {
21
22   /**
23    * The date formatter service.
24    *
25    * @var \Drupal\Core\Datetime\DateFormatterInterface
26    */
27   protected $dateFormatter;
28
29   /**
30    * Constructs a new NodeListBuilder object.
31    *
32    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
33    *   The entity type definition.
34    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
35    *   The entity storage class.
36    * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
37    *   The date formatter service.
38    */
39   public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
40     parent::__construct($entity_type, $storage);
41
42     $this->dateFormatter = $date_formatter;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
49     return new static(
50       $entity_type,
51       $container->get('entity.manager')->getStorage($entity_type->id()),
52       $container->get('date.formatter')
53     );
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function buildHeader() {
60     $header = [
61       'word' => $this->t('Word'),
62       'synonyms' => $this->t('Synonyms'),
63       'author' => [
64         'data' => $this->t('Author'),
65         'class' => [RESPONSIVE_PRIORITY_LOW],
66       ],
67       'status' => [
68         'data' => $this->t('Status'),
69         'class' => [RESPONSIVE_PRIORITY_LOW],
70       ],
71       'changed' => [
72         'data' => $this->t('Updated'),
73         'class' => [RESPONSIVE_PRIORITY_LOW],
74       ],
75     ];
76     if (\Drupal::languageManager()->isMultilingual()) {
77       $header['language_name'] = [
78         'data' => $this->t('Language'),
79         'class' => [RESPONSIVE_PRIORITY_LOW],
80       ];
81     }
82
83     return $header + parent::buildHeader();
84   }
85
86   /**
87    * {@inheritdoc}
88    */
89   public function buildRow(EntityInterface $entity) {
90     $langcode = $entity->language()->getId();
91
92     /* @var $entity \Drupal\search_api_synonym\Entity\Synonym */
93     $row['word'] = $this->buildEditLink($entity->label(), $entity);
94     $row['synonyms'] = $this->buildEditLink($entity->getSynonyms(), $entity);
95     $row['author']['data'] = [
96       '#theme' => 'username',
97       '#account' => $entity->getOwner(),
98     ];
99     $row['status'] = $entity->isActive() ? $this->t('active') : $this->t('inactive');
100     $row['changed'] = $this->dateFormatter->format($entity->getChangedTime(), 'short');
101     $language_manager = \Drupal::languageManager();
102     if ($language_manager->isMultilingual()) {
103       $row['language_name'] = $language_manager->getLanguageName($langcode);
104     }
105
106     return $row + parent::buildRow($entity);
107   }
108
109   /**
110    * Build the edit link object.
111    *
112    * @param string $label
113    *   The label used in the link
114    *
115    * @param \Drupal\search_api_synonym\Entity\Synonym $entity
116    *   The synonym entity object.
117    *
118    * @return \Drupal\Core\Link
119    *   The build link object.
120    */
121   private function buildEditLink($label, $entity) {
122     return new Link(
123       $label,
124       new Url(
125         'entity.search_api_synonym.edit_form', [
126           'search_api_synonym' => $entity->id(),
127         ]
128       )
129     );
130   }
131
132 }