b6f6921db43633e2324d9ce8ebecd0b25e07d282
[yaffs-website] / web / core / modules / taxonomy / src / VocabularyListBuilder.php
1 <?php
2
3 namespace Drupal\taxonomy;
4
5 use Drupal\Core\Config\Entity\DraggableListBuilder;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Messenger\MessengerInterface;
11 use Drupal\Core\Render\RendererInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Drupal\Core\Url;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Defines a class to build a listing of taxonomy vocabulary entities.
18  *
19  * @see \Drupal\taxonomy\Entity\Vocabulary
20  */
21 class VocabularyListBuilder extends DraggableListBuilder {
22
23   /**
24    * {@inheritdoc}
25    */
26   protected $entitiesKey = 'vocabularies';
27
28   /**
29    * The current user.
30    *
31    * @var \Drupal\Core\Session\AccountInterface
32    */
33   protected $currentUser;
34
35   /**
36    * The entity manager.
37    *
38    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39    */
40   protected $entityTypeManager;
41
42   /**
43    * The renderer service.
44    *
45    * @var \Drupal\Core\Render\RendererInterface
46    */
47   protected $renderer;
48
49   /**
50    * The messenger.
51    *
52    * @var \Drupal\Core\Messenger\MessengerInterface
53    */
54   protected $messenger;
55
56   /**
57    * Constructs a new VocabularyListBuilder object.
58    *
59    * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
60    *   The entity type definition.
61    * @param \Drupal\Core\Session\AccountInterface $current_user
62    *   The current user.
63    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
64    *   The entity manager service.
65    * @param \Drupal\Core\Render\RendererInterface $renderer
66    *   The renderer service.
67    * @param \Drupal\Core\Messenger\MessengerInterface $messenger
68    *   The messenger.
69    */
70   public function __construct(EntityTypeInterface $entity_type,
71                               AccountInterface $current_user,
72                               EntityTypeManagerInterface $entity_type_manager,
73                               RendererInterface $renderer = NULL,
74                               MessengerInterface $messenger) {
75     parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id()));
76
77     $this->currentUser = $current_user;
78     $this->entityTypeManager = $entity_type_manager;
79     $this->renderer = $renderer;
80     $this->messenger = $messenger;
81   }
82
83   /**
84    * {@inheritdoc}
85    */
86   public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
87     return new static(
88       $entity_type,
89       $container->get('current_user'),
90       $container->get('entity_type.manager'),
91       $container->get('renderer'),
92       $container->get('messenger')
93     );
94   }
95
96   /**
97    * {@inheritdoc}
98    */
99   public function getFormId() {
100     return 'taxonomy_overview_vocabularies';
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public function getDefaultOperations(EntityInterface $entity) {
107     $operations = parent::getDefaultOperations($entity);
108
109     if (isset($operations['edit'])) {
110       $operations['edit']['title'] = t('Edit vocabulary');
111     }
112
113     if ($entity->access('access taxonomy overview')) {
114       $operations['list'] = [
115         'title' => t('List terms'),
116         'weight' => 0,
117         'url' => $entity->toUrl('overview-form'),
118       ];
119     }
120
121     $taxonomy_term_access_control_handler = $this->entityTypeManager->getAccessControlHandler('taxonomy_term');
122     if ($taxonomy_term_access_control_handler->createAccess($entity->id())) {
123       $operations['add'] = [
124         'title' => t('Add terms'),
125         'weight' => 10,
126         'url' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $entity->id()]),
127       ];
128     }
129
130     unset($operations['delete']);
131
132     return $operations;
133   }
134
135   /**
136    * {@inheritdoc}
137    */
138   public function buildHeader() {
139     $header['label'] = t('Vocabulary name');
140     $header['description'] = t('Description');
141
142     if ($this->currentUser->hasPermission('administer vocabularies') && !empty($this->weightKey)) {
143       $header['weight'] = t('Weight');
144     }
145
146     return $header + parent::buildHeader();
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function buildRow(EntityInterface $entity) {
153     $row['label'] = $entity->label();
154     $row['description']['data'] = ['#markup' => $entity->getDescription()];
155     return $row + parent::buildRow($entity);
156   }
157
158   /**
159    * {@inheritdoc}
160    */
161   public function render() {
162     $entities = $this->load();
163     // If there are not multiple vocabularies, disable dragging by unsetting the
164     // weight key.
165     if (count($entities) <= 1) {
166       unset($this->weightKey);
167     }
168     $build = parent::render();
169
170     // If the weight key was unset then the table is in the 'table' key,
171     // otherwise in vocabularies. The empty message is only needed if the table
172     // is possibly empty, so there is no need to support the vocabularies key
173     // here.
174     if (isset($build['table'])) {
175       $access_control_handler = $this->entityTypeManager->getAccessControlHandler('taxonomy_vocabulary');
176       $create_access = $access_control_handler->createAccess(NULL, NULL, [], TRUE);
177       $this->renderer->addCacheableDependency($build['table'], $create_access);
178       if ($create_access->isAllowed()) {
179         $build['table']['#empty'] = t('No vocabularies available. <a href=":link">Add vocabulary</a>.', [
180           ':link' => Url::fromRoute('entity.taxonomy_vocabulary.add_form')->toString(),
181         ]);
182       }
183       else {
184         $build['table']['#empty'] = t('No vocabularies available.');
185       }
186     }
187
188     return $build;
189   }
190
191   /**
192    * {@inheritdoc}
193    */
194   public function buildForm(array $form, FormStateInterface $form_state) {
195     $form = parent::buildForm($form, $form_state);
196     $form['vocabularies']['#attributes'] = ['id' => 'taxonomy'];
197     $form['actions']['submit']['#value'] = t('Save');
198
199     return $form;
200   }
201
202   /**
203    * {@inheritdoc}
204    */
205   public function submitForm(array &$form, FormStateInterface $form_state) {
206     parent::submitForm($form, $form_state);
207
208     $this->messenger->addStatus($this->t('The configuration options have been saved.'));
209   }
210
211 }