backdrop-issues icon indicating copy to clipboard operation
backdrop-issues copied to clipboard

Layout API: Provide contexts to custom blocks

Open argiepiano opened this issue 3 years ago • 4 comments

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.

argiepiano avatar Jul 01 '22 19:07 argiepiano

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.

docwilmot avatar Jul 03 '22 18:07 docwilmot

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.

quicksketch avatar Sep 12 '22 20:09 quicksketch

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.

argiepiano avatar Sep 29 '22 00:09 argiepiano

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

Screen Shot 2022-09-28 at 7 58 18 PM Screen Shot 2022-09-28 at 7 58 33 PM

argiepiano avatar Sep 29 '22 01:09 argiepiano

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?

docwilmot avatar Mar 11 '23 14:03 docwilmot