3 namespace Drupal\taxonomy;
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;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
17 * Defines a class to build a listing of taxonomy vocabulary entities.
19 * @see \Drupal\taxonomy\Entity\Vocabulary
21 class VocabularyListBuilder extends DraggableListBuilder {
26 protected $entitiesKey = 'vocabularies';
31 * @var \Drupal\Core\Session\AccountInterface
33 protected $currentUser;
38 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40 protected $entityTypeManager;
43 * The renderer service.
45 * @var \Drupal\Core\Render\RendererInterface
52 * @var \Drupal\Core\Messenger\MessengerInterface
57 * Constructs a new VocabularyListBuilder object.
59 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
60 * The entity type definition.
61 * @param \Drupal\Core\Session\AccountInterface $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
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()));
77 $this->currentUser = $current_user;
78 $this->entityTypeManager = $entity_type_manager;
79 $this->renderer = $renderer;
80 $this->messenger = $messenger;
86 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
89 $container->get('current_user'),
90 $container->get('entity_type.manager'),
91 $container->get('renderer'),
92 $container->get('messenger')
99 public function getFormId() {
100 return 'taxonomy_overview_vocabularies';
106 public function getDefaultOperations(EntityInterface $entity) {
107 $operations = parent::getDefaultOperations($entity);
109 if (isset($operations['edit'])) {
110 $operations['edit']['title'] = t('Edit vocabulary');
113 if ($entity->access('access taxonomy overview')) {
114 $operations['list'] = [
115 'title' => t('List terms'),
117 'url' => $entity->toUrl('overview-form'),
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'),
126 'url' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $entity->id()]),
130 unset($operations['delete']);
138 public function buildHeader() {
139 $header['label'] = t('Vocabulary name');
140 $header['description'] = t('Description');
142 if ($this->currentUser->hasPermission('administer vocabularies') && !empty($this->weightKey)) {
143 $header['weight'] = t('Weight');
146 return $header + parent::buildHeader();
152 public function buildRow(EntityInterface $entity) {
153 $row['label'] = $entity->label();
154 $row['description']['data'] = ['#markup' => $entity->getDescription()];
155 return $row + parent::buildRow($entity);
161 public function render() {
162 $entities = $this->load();
163 // If there are not multiple vocabularies, disable dragging by unsetting the
165 if (count($entities) <= 1) {
166 unset($this->weightKey);
168 $build = parent::render();
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
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(),
184 $build['table']['#empty'] = t('No vocabularies available.');
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');
205 public function submitForm(array &$form, FormStateInterface $form_state) {
206 parent::submitForm($form, $form_state);
208 $this->messenger->addStatus($this->t('The configuration options have been saved.'));