Currently, contexts in page_manager work by subscribing to an event and adding the context value directly to the Page. This binds the context provider directly to page_manager, and can't be reused by other things needing context.
In core, there is LazyContextRepository which collects tagged services (with the "context_provider" tag) and asks them to provide contexts on demand. This is sufficiently generic and could be reused by page_manager, or anything else.
However, this issue is to create a central, stateful context service (the "context stack"), and things that have context to register will proactively register their contexts with it as they go about their business. Since core doesn't do this, it will be necessary to replace some core services with versions that register their contexts.
| Comment | File | Size | Author |
|---|---|---|---|
| #15 | 2511568-15-page_manager.patch | 11.72 KB | rlmumford |
| #15 | 2511568-15.patch | 29.39 KB | rlmumford |
Comments
Comment #1
larowlanThis aligns with teething problems in core, going to try and make it generic enough to support both
Comment #2
dsnopekA quick question for something that has been nagging at me: why are we calling this a "stack"?
When I think of "stack", I think of this:
Stack (abstract data type)
And, I can't imagine a way for this to work that fits that description. Could we (like core) switch calling this a "context manager"? As it is, I feel icky typing "context stack" and always have to put it in quotes to feel a little better about it. :-)
Or am I misunderstanding something fundamental about what we're envisioning here? (Mostly a question to EclipseGC!)
Comment #3
rlmumfordDid some work on this as a proof of concept.
Introduced a ContextManager class that uses an Event and Event subscribers to collate contexts.
Introduced a ContextManagerPlugin type that allows the ContextMAnager and EventSubscribers to get the information they need to build the context. For example; to get the staticContext configuration from the page entity, page_manager will write a plugin and then pass that plugin to the ContextManager.
I have attached two patches, the first for ctools and the second for the page_manager module, showing how another module might use this code. The page_manager patch makes a lot of code in page_manager redundant, but doesn't remove that code.
N.B. The ctools patch currently adds a dependency on page_manager because the EntityLazyLoadContext still lives there.
Comment #4
rlmumfordAdding related issue #2571699: Include EntityLazyLoadContext from Page Manager
Comment #5
dsnopekThis turned out not to be needed by #2511554: [meta] Move some parts of Page Manager into CTools so I'm removing that as parent of this issue
Comment #6
dsnopekUpdated issue summary to reflect the current state of core (since it switched to using the
LazyContextRepository) and some notes about the implementation that EclipseGC had in mind from IRC.I'm still not personally aware of the problem that this "context stack" solves which prevents us from just using the
LazyContextRpository, so I'd love for EclipseGC or tim.plunkett to explain that on this issue at some point. However, I trust that they know what they're doing. :-)Comment #7
berdirI've been thinking about this quite a bit since talking with @timplunkett about the static context refactoring a few days ago. Writing down my thoughts for now:
First, I believe the lazy load/initialization approach provided by core is a good way to do this. Note that there's still a way to get all context that could exist with getAvailableContexts(). But that's only for administrative purposes, when configuration contexts and not in the critical path. We could very likely end up with quite a few contexts in D8, e.g. if we think on making information that e.g. tokens have also available as context. We don't want to initialize a dozen services and their dependencies just because something wants the current user context.
However, the core service doesn't work for page_manager for example. The difference is that for page manager, context is collected based on/around an object.. the page and/or display entity. The core service has no way to handle that. We explicitly excluded it because we wanted to avoid some pushing an incomplete/untested (as in real world use cases) API into core. The generic name I've come up with for such an object is "context environment" / ContextEnvironmentInterface. That interface has a number of methods to do/provide:
* Already instantiated contexts and always available default context for that object ( replacing a global static cache, already loaded contexts would be stored on the object, more or less like they are now)
* static context definitions (probably as context definition objects?)
* relationships
* ... ?
You'd call that service with a list of contexts that you need and an environment. That would then do whatever's necessary to provide them for you. load from services, load based on static context definitions, resolve relationships (especially that sounds like a fair amount of non-trivial logic. you might request a context provided by relationship, that in turn depends on a static context, ...)
Note that to be able to do the lazy-loading efficiently, we need to make the same API change for context names that core already did. contexts, at least those provided by services, must be namespaced with the service name, so we can instantiate that service when needed, and only then. I guess static context and relationships don't have such a restriction, although we might want to force a namespace on them too (e.g. @static:some_name).
The context environment would be passed around also to the services, so we also need an additional interface there. Such a service, e.g. RouteParamContext could then check if a given passed in context environment (I always type twig environment..) is in fact a Page and if so, return the appropriate context.
Our service could also fall back to the core service for global context like current user. So we don't have to duplicate that. Might also pollute our UI's, though (do we really need the current node context, for example?)
I still have a number of things that I'm not sure about:
* Would this be the API that you call and no longer getContexts() which would just be about returning already loaded contexts?
* Not yet sure how to deal with the double-enviroment Page/Display. I guess a Display would have to transparently decorate the page and also allow access to contexts already loaded there. But how would e.g. the RouteParamContext interact with it? Explicitly, maybe. Not so nice, but easy to implement.
* Should we replace the core service or provide a separate one that just has the core one as a dependency?
* static contexts/relationships might not even have to be hardcoded within that. It could just be a service like any other that requests data from a specific interface like a display. Then we wouldn't have to lock us into generic API's and specific implementations for resolving relationships. But even if we do, someone could still do this to provide complex relationships that do more than what our basic typed data based relationship resolver can handle.
* I had more things that I was thinking about but I forgot them again I think...
If this doesn't sound completely crazy then I'll try to come up with a proof of concept to get this started. Also looking for feedback on the naming, specifically the environment thing.
Comment #8
larowlanNot sure this is the best place but https://gist.github.com/larowlan/dc88c7e2cd2d816fe80d99a8c92160b3 is a generic 'entity from route' context provider.
Couldn't find anything pre-existing.
Have left a tell for EclipseGc to see if its worth inclusion in ctools
Comment #9
decipheredThanks @larowlan,
Without that, one can't place a block on a Media page, or similar contextual placement.
Comment #10
japerryComment #11
damienmckennaPunting 'til later due to inactivity.
Comment #12
rlmumfordDone some work here on an environment aware context repository. It's not a service at the moment.
EnvironmentAwareContextRepository stores a list of environments keyed by a name. Environments can then be registered added to the context repository and make their contexts available, these are then given the id @env:{environment_name}:{unqualified_context_id}.
ContextEnvironmentInterface defines two methods:
getRuntimeEnvironmentContexts()
getAvailableEnvironmentContexts()
These are similar to those methods defined in ContextRepositoryInterface with the exception that they return unqualified context ids specific to the particular environment.
There are also some traits designed to make it easier for modules providing context environments.
I have also attached a patch for how this could be implemented in page_manager.
Questions:
1. EnvironmentAwareContextRepository as a service? We could register environments against some global service, however I'm not sure how this would work when it came to Mini-Panels/Block Context Environments. What if the same block was used twice on a page with different provided context? It would need two ContextEnvironments registered in the service, unless we somehow unregistered the relevant environments once execution was complete.
2. Unqualified Context Names? In this code I've used @env:{environment_name}:{unqualified_context_id}. Is this too long (particularly for use in tokens)? Do we need to use an environment name in the fully qualified id or is '@env' enough. Without the environment name, environments could override each others contexts, this might be a desired feature but we would need to implement some kind of environment order.
Comment #13
rlmumfordHere is the Ctools patch
Comment #14
rlmumfordThis round of patches adds a factory service to create EnvironmentAwareContextRepositories.
Comment #15
rlmumfordChange of approach on this. I now register EnvironmentContextProvider objects with the EnvironmentAwareContextRepository object. You can see that the PageVariant entity takes the Page's repository and adds two providers to it.
Putting this to needs review for discussion of concept.
Comment #17
rlmumfordSetting back to needs review to discuss the approach.
Comment #21
darrenwh commentedSetting to need work, its failing to apply
Comment #22
rlmumford@darrenwh - one of the patches there is for Page Manager - not CTools (hence the failure to apply). Does the CTools one not apply anymore?
Comment #23
andypostComment #24
japerry