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\Render\RendererInterface;
11 use Drupal\Core\Session\AccountInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Defines a class to build a listing of taxonomy vocabulary entities.
18 * @see \Drupal\taxonomy\Entity\Vocabulary
20 class VocabularyListBuilder extends DraggableListBuilder {
25 protected $entitiesKey = 'vocabularies';
30 * @var \Drupal\Core\Session\AccountInterface
32 protected $currentUser;
37 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
39 protected $entityTypeManager;
42 * The renderer service.
44 * @var \Drupal\Core\Render\RendererInterface
49 * Constructs a new VocabularyListBuilder object.
51 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
52 * The entity type definition.
53 * @param \Drupal\Core\Session\AccountInterface $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.
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()));
63 $this->currentUser = $current_user;
64 $this->entityTypeManager = $entity_type_manager;
65 $this->renderer = $renderer;
71 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
74 $container->get('current_user'),
75 $container->get('entity_type.manager'),
76 $container->get('renderer')
83 public function getFormId() {
84 return 'taxonomy_overview_vocabularies';
90 public function getDefaultOperations(EntityInterface $entity) {
91 $operations = parent::getDefaultOperations($entity);
93 if (isset($operations['edit'])) {
94 $operations['edit']['title'] = t('Edit vocabulary');
97 if ($entity->access('access taxonomy overview')) {
98 $operations['list'] = [
99 'title' => t('List terms'),
101 'url' => $entity->toUrl('overview-form'),
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'),
110 'url' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $entity->id()]),
114 unset($operations['delete']);
122 public function buildHeader() {
123 $header['label'] = t('Vocabulary name');
124 $header['description'] = t('Description');
126 if ($this->currentUser->hasPermission('administer vocabularies')) {
127 $header['weight'] = t('Weight');
130 return $header + parent::buildHeader();
136 public function buildRow(EntityInterface $entity) {
137 $row['label'] = $entity->label();
138 $row['description']['data'] = ['#markup' => $entity->getDescription()];
139 return $row + parent::buildRow($entity);
145 public function render() {
146 $entities = $this->load();
147 // If there are not multiple vocabularies, disable dragging by unsetting the
149 if (count($entities) <= 1) {
150 unset($this->weightKey);
152 $build = parent::render();
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
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()
168 $build['table']['#empty'] = t('No vocabularies available.');
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');
189 public function submitForm(array &$form, FormStateInterface $form_state) {
190 parent::submitForm($form, $form_state);
192 drupal_set_message(t('The configuration options have been saved.'));