3 namespace Drupal\search_api_synonym;
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;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Defines a class to build a listing of Synonym entities.
18 * @ingroup search_api_synonym
20 class SynonymListBuilder extends EntityListBuilder {
23 * The date formatter service.
25 * @var \Drupal\Core\Datetime\DateFormatterInterface
27 protected $dateFormatter;
30 * Constructs a new NodeListBuilder object.
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.
39 public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, DateFormatterInterface $date_formatter) {
40 parent::__construct($entity_type, $storage);
42 $this->dateFormatter = $date_formatter;
48 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
51 $container->get('entity.manager')->getStorage($entity_type->id()),
52 $container->get('date.formatter')
59 public function buildHeader() {
61 'word' => $this->t('Word'),
62 'synonyms' => $this->t('Synonyms'),
64 'data' => $this->t('Author'),
65 'class' => [RESPONSIVE_PRIORITY_LOW],
68 'data' => $this->t('Status'),
69 'class' => [RESPONSIVE_PRIORITY_LOW],
72 'data' => $this->t('Updated'),
73 'class' => [RESPONSIVE_PRIORITY_LOW],
76 if (\Drupal::languageManager()->isMultilingual()) {
77 $header['language_name'] = [
78 'data' => $this->t('Language'),
79 'class' => [RESPONSIVE_PRIORITY_LOW],
83 return $header + parent::buildHeader();
89 public function buildRow(EntityInterface $entity) {
90 $langcode = $entity->language()->getId();
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(),
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);
106 return $row + parent::buildRow($entity);
110 * Build the edit link object.
112 * @param string $label
113 * The label used in the link
115 * @param \Drupal\search_api_synonym\Entity\Synonym $entity
116 * The synonym entity object.
118 * @return \Drupal\Core\Link
119 * The build link object.
121 private function buildEditLink($label, $entity) {
125 'entity.search_api_synonym.edit_form', [
126 'search_api_synonym' => $entity->id(),