annotate core/modules/taxonomy/src/VocabularyListBuilder.php @ 16:c2387f117808

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