Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
BlockViewBuilder::viewMultiple()
(BVB) generates a render array for a block that gets some information from the block configuration entity, some from the block plugin, and some from the internal logic of BVB.- BVB invokes an alter hook to then allow modules to alter that result.
- After #2459819: Remove CacheableInterface (and no longer let block plugins implement it), we have a use case for block plugins themselves wanting to alter the BVB generated array. For example, the SystemPoweredByBlock is cheaper to render than the cost of a cache_get(), so it may want to unset
#cache['keys']
in order to optimize away the caching of itself on its own. Note that it does not and should not do this by implementing getCacheMaxAge() of 0, because the contents are cacheable, and it's necessary for higher-level caches, such as the page cache to know that. So unsetting the cache keys isn't about informing the system that the contents are uncacheable, but about optimizing away a decision to cache it at that particular level. - There might be other use cases for a block plugin to alter the BVB array? Perhaps changing
#theme
? - The above use cases can be implemented via the alter hook, but that then places the implementation in a separate place (e.g., in a .module file) from the plugin.
Proposed resolution
?
Remaining tasks
Discuss.
Comments
Comment #1
effulgentsia CreditAttribution: effulgentsia at Acquia commentedI opened this issue per #2459819-26: Remove CacheableInterface (and no longer let block plugins implement it), but so far I'm -1 on it. Or rather, my current opinion on the "decide if" is "no".
I think it violates separation of concerns for the plugin to alter what BVB generates, since per the issue summary, multiple actors participate in that generation. I think even using such an alter method to unset #cache['keys'] would be a violation of that, since the plugin doesn't know the cache strategy employed by the callers. For example, what if all cacheable blocks were retrieved with a cache_get_multiple()?