comparison core/modules/taxonomy/src/VocabularyListBuilder.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
2 2
3 namespace Drupal\taxonomy; 3 namespace Drupal\taxonomy;
4 4
5 use Drupal\Core\Config\Entity\DraggableListBuilder; 5 use Drupal\Core\Config\Entity\DraggableListBuilder;
6 use Drupal\Core\Entity\EntityInterface; 6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityTypeInterface;
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Form\FormStateInterface; 9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Render\RendererInterface;
11 use Drupal\Core\Session\AccountInterface;
8 use Drupal\Core\Url; 12 use Drupal\Core\Url;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
9 14
10 /** 15 /**
11 * Defines a class to build a listing of taxonomy vocabulary entities. 16 * Defines a class to build a listing of taxonomy vocabulary entities.
12 * 17 *
13 * @see \Drupal\taxonomy\Entity\Vocabulary 18 * @see \Drupal\taxonomy\Entity\Vocabulary
16 21
17 /** 22 /**
18 * {@inheritdoc} 23 * {@inheritdoc}
19 */ 24 */
20 protected $entitiesKey = 'vocabularies'; 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 }
21 79
22 /** 80 /**
23 * {@inheritdoc} 81 * {@inheritdoc}
24 */ 82 */
25 public function getFormId() { 83 public function getFormId() {
34 92
35 if (isset($operations['edit'])) { 93 if (isset($operations['edit'])) {
36 $operations['edit']['title'] = t('Edit vocabulary'); 94 $operations['edit']['title'] = t('Edit vocabulary');
37 } 95 }
38 96
39 $operations['list'] = [ 97 if ($entity->access('access taxonomy overview')) {
40 'title' => t('List terms'), 98 $operations['list'] = [
41 'weight' => 0, 99 'title' => t('List terms'),
42 'url' => $entity->urlInfo('overview-form'), 100 'weight' => 0,
43 ]; 101 'url' => $entity->toUrl('overview-form'),
44 $operations['add'] = [ 102 ];
45 'title' => t('Add terms'), 103 }
46 'weight' => 10, 104
47 'url' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $entity->id()]), 105 $taxonomy_term_access_control_handler = $this->entityTypeManager->getAccessControlHandler('taxonomy_term');
48 ]; 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
49 unset($operations['delete']); 114 unset($operations['delete']);
50 115
51 return $operations; 116 return $operations;
52 } 117 }
53 118
55 * {@inheritdoc} 120 * {@inheritdoc}
56 */ 121 */
57 public function buildHeader() { 122 public function buildHeader() {
58 $header['label'] = t('Vocabulary name'); 123 $header['label'] = t('Vocabulary name');
59 $header['description'] = t('Description'); 124 $header['description'] = t('Description');
125
126 if ($this->currentUser->hasPermission('administer vocabularies')) {
127 $header['weight'] = t('Weight');
128 }
129
60 return $header + parent::buildHeader(); 130 return $header + parent::buildHeader();
61 } 131 }
62 132
63 /** 133 /**
64 * {@inheritdoc} 134 * {@inheritdoc}
78 // weight key. 148 // weight key.
79 if (count($entities) <= 1) { 149 if (count($entities) <= 1) {
80 unset($this->weightKey); 150 unset($this->weightKey);
81 } 151 }
82 $build = parent::render(); 152 $build = parent::render();
83 $build['table']['#empty'] = t('No vocabularies available. <a href=":link">Add vocabulary</a>.', [':link' => \Drupal::url('entity.taxonomy_vocabulary.add_form')]); 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
84 return $build; 172 return $build;
85 } 173 }
86 174
87 /** 175 /**
88 * {@inheritdoc} 176 * {@inheritdoc}