Several entity operation based methods have added a new \Drupal\Core\Cache\CacheableMetadata $cacheability parameter:
\Drupal\Core\Entity\EntityListBuilder::getOperations()
\Drupal\Core\Entity\EntityListBuilder::getDefaultOperations()
hook_entity_operation
hook_entity_operation_alter
This is especially useful for adding cacheable metadata from access checks for operations. EntityListBuilder now does this by default for the edit and delete operations.
For hooks, the new parameter can be added to new and existing implementations:
/**
* Implements hook_entity_operation().
*/
#[Hook('entity_operation')]
public function entityOperation(\Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Cache\CacheableMetadata $cacheability): array {
$access = $entity->access('foo', NULL, TRUE);
$cacheability->addCacheableDependency($access);
if ($access->isAllowed()) {
$operations['my_operation'] = [
'title' => 'My operation',
'weight' => 20,
'url' => $entity->toUrl('bar'),
];
}
}
Views and EntityListBuilder also automatically applies this cacheable metadata to the render output of its Operations field.
Known limitations
getDefaultOperations() does not bubble up cacheability information from operations in multiple built-in entity list builders.