Problem/Motivation
In our installation profile, we have some views that need to vary with a cache context provided by an access control module that is optional; in other words, clients who use the profile can choose whether they want that access control module enabled to enforce access control that way, or leave it disabled (the default), in which case all users on the system have access to content.
Unfortunately, that means that views in the platform need to be exported with a cache context provided by the module so that, when it is enabled, the proper content is shown to each user relative to their permissions. We have discovered that when we do this, though, and the module is not enabled, the site no longer functions because the caching system assumes the cache context will always be available.
We get this error:
Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException: You have requested a non-existent service "cache_context.user.custom_grants".
Steps to reproduce
- Create a custom module that defines a new cache context service called "cache_context.user.custom_grants".
- In the new custom module, define a
hook_entity_type_alter()implementation that adds the new cache context to thelist_cache_contextsdefinition of taxonomy terms (so that taxonomy term listings take the new cache context into consideration). - Create a new view that lists taxonomy terms.
- Export site configuration.
- Confirm that the exported configuration for the view from step 3 includes the new cache context.
- Package site configuration up into an installation profile that does not enable the custom module from step 1 by default.
- Install a site from the install profile.
- Attempt to use the view created in step 3 in the new site.
The view won't load because the "cache_context.user.custom_grants" cache context service isn't available.
Proposed resolution
The cache system should skip over contexts that are not defined; OR, I suppose, views could filter out cache contexts that are not valid when it is loading a view from config.
Comments
Comment #2
larowlanIf this is an install profile you can add a forced dependency on the module to the view by editing the yml, see node.type.forum.yml in core for an example
Then you put the view in the profiles config/optional folder and it gets enabled when the module is enabled
Comment #3
guypaddock commented@larowlan: the views aren't optional; they're always expected to be installed. As I explained in the write-up, the module is also optional; the problem arises because caching has to be handled differently (more contexts) when the optional module is installed.
Comment #4
larowlanCould you handle that with hook_modules_installed?