Layout API: Provide contexts to custom blocks
Description of the need
If a Layout made its contexts available to its custom blocks, this would allow people to use tokens related to the entities in the context. Use case: create a custom block that displays some node properties like the created date, which are otherwise not selectively available.
Currently the layout contexts are not available to BlockText, which is the class that custom blocks use. While BlockText::getContent() does invoke token_replace() it does so without any context, meaning that only global tokens are fulfilled.
Good point, that should have been the case from start. I'm tentatively calling this a bug, unless others disagree. Should be a simple quick fix.
I'm not sure this would be easy. Blocks right now either have "required context" or none at all. So if we make node a required context, the "BlockText" block would only be placeable on node/% layouts. Fulfilling this request means we would need to implement the conceptually more nebulous "optional context", meaning the block could work with or without a context provided.
Blocks right now either have "required context" or none at all. So if we make node a required context, the "BlockText" block would only be placeable on node/% layouts
Understood. Then I'd propose creating a "Token block" that has entity as required context. This would allow people to use tokens for those entities. I'll try creating this as a contrib module first to see if this is feasible/desirable.
I just released a contrib module that allows for token replacement for entities that are part of the layout context. For example, if you use a path `node/%' you will see a new block called "Tokens for node entities" that provides a text box where you can type tokens for that particular node.
https://github.com/backdrop-contrib/token_block
Could we just add the logic to the block config form though? Load the Layout in the hook_block_configure() then call getContexts() and then set token_types based on the contexts?