diff --git a/core/lib/Drupal/Core/Entity/EntityListBuilder.php b/core/lib/Drupal/Core/Entity/EntityListBuilder.php index 3039c4f8eb..a0b011262a 100644 --- a/core/lib/Drupal/Core/Entity/EntityListBuilder.php +++ b/core/lib/Drupal/Core/Entity/EntityListBuilder.php @@ -2,6 +2,7 @@ namespace Drupal\Core\Entity; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\Messenger\MessengerTrait; use Drupal\Core\Routing\RedirectDestinationTrait; use Drupal\Core\Url; @@ -145,18 +146,24 @@ public function getOperations(EntityInterface $entity) { */ protected function getDefaultOperations(EntityInterface $entity) { $operations = []; - if ($entity->access('update') && $entity->hasLinkTemplate('edit-form')) { + if ($entity->hasLinkTemplate('edit-form')) { + $update_access = $entity->access('update', NULL, TRUE); $operations['edit'] = [ 'title' => $this->t('Edit'), 'weight' => 10, 'url' => $this->ensureDestination($entity->toUrl('edit-form')), + 'access' => $update_access->isAllowed(), + 'cache' => CacheableMetadata::createFromObject($update_access), ]; } - if ($entity->access('delete') && $entity->hasLinkTemplate('delete-form')) { + if ($entity->hasLinkTemplate('delete-form')) { + $delete_access = $entity->access('delete', NULL, TRUE); $operations['delete'] = [ 'title' => $this->t('Delete'), 'weight' => 100, 'url' => $this->ensureDestination($entity->toUrl('delete-form')), + 'access' => $delete_access->isAllowed(), + 'cache' => CacheableMetadata::createFromObject($delete_access), ]; } diff --git a/core/modules/views/src/Plugin/views/field/EntityOperations.php b/core/modules/views/src/Plugin/views/field/EntityOperations.php index 32a30ad1ae..b4b7f9470a 100644 --- a/core/modules/views/src/Plugin/views/field/EntityOperations.php +++ b/core/modules/views/src/Plugin/views/field/EntityOperations.php @@ -2,6 +2,8 @@ namespace Drupal\views\Plugin\views\field; +use Drupal\Core\Cache\CacheableDependencyInterface; +use Drupal\Core\Cache\CacheableMetadata; use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait; use Drupal\Core\Entity\EntityRepositoryInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; @@ -141,6 +143,15 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { public function render(ResultRow $values) { $entity = $this->getEntityTranslation($this->getEntity($values), $values); $operations = $this->entityTypeManager->getListBuilder($entity->getEntityTypeId())->getOperations($entity); + $cacheability = new CacheableMetadata(); + foreach ($operations as $operation) { + if (isset($operation['cache']) && $operation['cache'] instanceof CacheableDependencyInterface) { + $cacheability->addCacheableDependency($operation['cache']); + } + } + $operations = array_filter($operations, function ($operation) { + return !isset($operation['access']) || $operation['access'] === TRUE; + }); if ($this->options['destination']) { foreach ($operations as &$operation) { if (!isset($operation['query'])) { @@ -153,6 +164,7 @@ public function render(ResultRow $values) { '#type' => 'operations', '#links' => $operations, ]; + $cacheability->applyTo($build); return $build; }