diff --git a/core/modules/forum/src/Form/Overview.php b/core/modules/forum/src/Form/Overview.php index c581802998..0262eee489 100644 --- a/core/modules/forum/src/Form/Overview.php +++ b/core/modules/forum/src/Form/Overview.php @@ -56,11 +56,6 @@ public function buildForm(array $form, FormStateInterface $form_state) { // Build base taxonomy term overview. $form = parent::buildForm($form, $form_state, $vocabulary); - // Ensure there is always an operations column. - if (count($form['terms']['#header']) === 1) { - $form['terms']['#header'][] = $this->t('Operations'); - } - foreach (Element::children($form['terms']) as $key) { if (isset($form['terms'][$key]['#term'])) { /** @var \Drupal\taxonomy\TermInterface $term */ diff --git a/core/modules/taxonomy/src/Form/OverviewTerms.php b/core/modules/taxonomy/src/Form/OverviewTerms.php index 2ac0f1f5ed..f22cfd5f45 100644 --- a/core/modules/taxonomy/src/Form/OverviewTerms.php +++ b/core/modules/taxonomy/src/Form/OverviewTerms.php @@ -2,10 +2,12 @@ namespace Drupal\taxonomy\Form; +use Drupal\Core\Access\AccessResult; use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Form\FormBase; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Url; use Drupal\taxonomy\VocabularyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -44,18 +46,28 @@ class OverviewTerms extends FormBase { protected $termListBuilder; /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** * Constructs an OverviewTerms object. * * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler * The module handler service. * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager service. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer service. */ - public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager) { + public function __construct(ModuleHandlerInterface $module_handler, EntityManagerInterface $entity_manager, RendererInterface $renderer = NULL) { $this->moduleHandler = $module_handler; $this->entityManager = $entity_manager; $this->storageController = $entity_manager->getStorage('taxonomy_term'); $this->termListBuilder = $entity_manager->getListBuilder('taxonomy_term'); + $this->renderer = $renderer ?: \Drupal::service('renderer'); } /** @@ -64,7 +76,8 @@ public function __construct(ModuleHandlerInterface $module_handler, EntityManage public static function create(ContainerInterface $container) { return new static( $container->get('module_handler'), - $container->get('entity.manager') + $container->get('entity.manager'), + $container->get('renderer') ); } @@ -216,7 +229,8 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular $row_position = 0; // Build the actual form. $access_control_handler = $this->entityManager->getAccessControlHandler('taxonomy_term'); - if ($access_control_handler->createAccess($taxonomy_vocabulary->id())) { + $create_access = $access_control_handler->createAccess($taxonomy_vocabulary->id(), NULL, [], TRUE); + if ($create_access->isAllowed()) { $empty = $this->t('No terms available. Add term.', ['@link' => Url::fromRoute('entity.taxonomy_term.add_form', ['taxonomy_vocabulary' => $taxonomy_vocabulary->id()])->toString()]); } else { @@ -229,12 +243,11 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular 'id' => 'taxonomy', ], ]; + $this->renderer->addCacheableDependency($form['terms'], $create_access); // Only allow access to changing weights if the user has update access for - // all terms, show operations if the user is allowed to see any operation - // for at least one term. - $change_weight_access = TRUE; - $operations_access = FALSE; + // all terms. + $change_weight_access = AccessResult::allowed(); foreach ($current_page as $key => $term) { /** @var $term \Drupal\Core\Entity\EntityInterface */ $term = $this->entityManager->getTranslationFromContext($term); @@ -280,10 +293,10 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular ], ]; } - $edit_access = $term->access('update'); - $change_weight_access &= $edit_access; + $update_access = $term->access('update', NULL, TRUE); + $change_weight_access = $change_weight_access->andIf($update_access); - if ($edit_access) { + if ($update_access->isAllowed()) { $form['terms'][$key]['weight'] = [ '#type' => 'weight', '#delta' => $delta, @@ -291,14 +304,10 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular '#title_display' => 'invisible', '#default_value' => $term->getWeight(), '#attributes' => ['class' => ['term-weight']], - '#access' => $edit_access, ]; } if ($operations = $this->termListBuilder->getOperations($term)) { - // Allow access to operations if there is at least one term with - // operations. - $operations_access = TRUE; $form['terms'][$key]['operations'] = [ '#type' => 'operations', '#links' => $operations, @@ -334,7 +343,9 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular } $form['terms']['#header'] = [$this->t('Name')]; - if ($change_weight_access) { + + $this->renderer->addCacheableDependency($form['terms'], $change_weight_access); + if ($change_weight_access->isAllowed()) { $form['terms']['#header'][] = $this->t('Weight'); if ($parent_fields) { $form['terms']['#tabledrag'][] = [ @@ -364,23 +375,19 @@ public function buildForm(array $form, FormStateInterface $form_state, Vocabular ]; } - if ($operations_access) { - $form['terms']['#header'][] = $this->t('Operations'); - } + $form['terms']['#header'][] = $this->t('Operations'); - if (($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && count($tree) > 1) && $change_weight_access) { + if (($taxonomy_vocabulary->getHierarchy() != VocabularyInterface::HIERARCHY_MULTIPLE && count($tree) > 1) && $change_weight_access->isAllowed()) { $form['actions'] = ['#type' => 'actions', '#tree' => FALSE]; $form['actions']['submit'] = [ '#type' => 'submit', '#value' => $this->t('Save'), '#button_type' => 'primary', - '#access' => $change_weight_access, ]; $form['actions']['reset_alphabetical'] = [ '#type' => 'submit', '#submit' => ['::submitReset'], '#value' => $this->t('Reset to alphabetical'), - '#access' => $change_weight_access, ]; } diff --git a/core/modules/taxonomy/src/TermListBuilder.php b/core/modules/taxonomy/src/TermListBuilder.php index 81d859120e..796057cda5 100644 --- a/core/modules/taxonomy/src/TermListBuilder.php +++ b/core/modules/taxonomy/src/TermListBuilder.php @@ -11,6 +11,9 @@ /** * Provides a taxonomy term list builder. + * + * @todo This is only necessary until https://www.drupal.org/node/2767857 + * provides the destination by default for edit and delete operations. */ class TermListBuilder extends EntityListBuilder { diff --git a/core/modules/taxonomy/src/VocabularyListBuilder.php b/core/modules/taxonomy/src/VocabularyListBuilder.php index d5304d2fc0..60d89958d3 100644 --- a/core/modules/taxonomy/src/VocabularyListBuilder.php +++ b/core/modules/taxonomy/src/VocabularyListBuilder.php @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; +use Drupal\Core\Render\RendererInterface; use Drupal\Core\Session\AccountInterface; use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -38,6 +39,13 @@ class VocabularyListBuilder extends DraggableListBuilder { protected $entityTypeManager; /** + * The renderer service. + * + * @var \Drupal\Core\Render\RendererInterface + */ + protected $renderer; + + /** * Constructs a new VocabularyListBuilder object. * * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type @@ -46,12 +54,15 @@ class VocabularyListBuilder extends DraggableListBuilder { * The current user. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity manager service. + * @param \Drupal\Core\Render\RendererInterface $renderer + * The renderer service. */ - public function __construct(EntityTypeInterface $entity_type, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) { + public function __construct(EntityTypeInterface $entity_type, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer = NULL) { parent::__construct($entity_type, $entity_type_manager->getStorage($entity_type->id())); $this->currentUser = $current_user; $this->entityTypeManager = $entity_type_manager; + $this->renderer = $renderer; } /** @@ -61,7 +72,8 @@ public static function createInstance(ContainerInterface $container, EntityTypeI return new static( $entity_type, $container->get('current_user'), - $container->get('entity_type.manager') + $container->get('entity_type.manager'), + $container->get('renderer') ); } @@ -138,13 +150,15 @@ public function render() { unset($this->weightKey); } $build = parent::render(); - $create_access = $this->entityTypeManager->getAccessControlHandler('taxonomy_vocabulary')->createAccess(); - if ($create_access) { + $create_access = $this->entityTypeManager->getAccessControlHandler('taxonomy_vocabulary')->createAccess(NULL, NULL, [], TRUE); + $this->renderer->addCacheableDependency($build['table'], $create_access); + if ($create_access->isAllowed()) { $build['table']['#empty'] = t('No vocabularies available. Add vocabulary.', ['@link' => Url::fromRoute('entity.taxonomy_vocabulary.add_form')->toString()]); } else { $build['table']['#empty'] = t('No vocabularies available.'); } + return $build; } diff --git a/core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php b/core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php index 5f0d545edd..aa6c02b0d2 100644 --- a/core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php +++ b/core/modules/taxonomy/tests/src/Functional/VocabularyPermissionsTest.php @@ -82,6 +82,7 @@ function testTaxonomyVocabularyOverviewPermissions() { $assert_session->linkExists('Delete'); $assert_session->linkExists('Add term'); $assert_session->buttonExists('Save'); + $assert_session->pageTextContains('Weight'); $assert_session->pageTextContains($edit_help_text); // Visit vocabulary overview without terms. 'Add term' should be shown. @@ -109,6 +110,7 @@ function testTaxonomyVocabularyOverviewPermissions() { $assert_session->linkNotExists('Edit'); $assert_session->linkNotExists('Delete'); $assert_session->buttonNotExists('Save'); + $assert_session->pageTextNotContains('Weight'); $assert_session->linkNotExists('Add term'); $assert_session->pageTextContains($no_edit_help_text); @@ -131,6 +133,7 @@ function testTaxonomyVocabularyOverviewPermissions() { $assert_session->linkExists('Edit'); $assert_session->linkNotExists('Delete'); $assert_session->buttonExists('Save'); + $assert_session->pageTextContains('Weight'); $assert_session->linkNotExists('Add term'); $assert_session->pageTextContains($edit_help_text); @@ -153,6 +156,7 @@ function testTaxonomyVocabularyOverviewPermissions() { $assert_session->linkExists('Delete'); $assert_session->linkNotExists('Add term'); $assert_session->buttonNotExists('Save'); + $assert_session->pageTextNotContains('Weight'); $assert_session->pageTextContains($no_edit_help_text); // Visit vocabulary overview without terms. 'Add term' should not be shown. @@ -176,6 +180,7 @@ function testTaxonomyVocabularyOverviewPermissions() { $assert_session->linkExists('Delete'); $assert_session->linkNotExists('Add term'); $assert_session->buttonExists('Save'); + $assert_session->pageTextContains('Weight'); $assert_session->pageTextContains($edit_help_text); // Visit vocabulary overview without terms. 'Add term' should not be shown. @@ -198,6 +203,7 @@ function testTaxonomyVocabularyOverviewPermissions() { $assert_session->linkNotExists('Delete'); $assert_session->linkExists('Add term'); $assert_session->buttonNotExists('Save'); + $assert_session->pageTextNotContains('Weight'); $assert_session->pageTextContains($no_edit_help_text); // Visit vocabulary overview without terms. 'Add term' should not be shown.