annotate core/modules/taxonomy/src/VocabularyListBuilder.php @ 19:fa3358dc1485 tip

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