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