Motivation
Drupal 8 has a lovely built-in capacity for custom blocks. Sure would be nice to be able to insert them into content like we can with normal blocks.
Proposed resolution
Add a new shortcode that pulls from custom blocks. The process is a bit more in-depth than your normal block because it's modeled a as an entity on the backend that must be rendered. The rendering itself has to be done somewhat manually; otherwise there is some lazy building that will break it.
Remaining tasks
Testing, reviewing, etc. There may be a cleaner way to render the block, and there may be some issues with how the block title is handled, so that should be reviewed.
API changes
New shortcode introduced into system for [block_content:block_id] where block_id is the numeric ID of the custom block.
This patch is rolled against 8.x-1.x, but is based off #2648496: Not working in Drupal 8 - Filter not Visible which gets this module working correctly for D8.
Comment | File | Size | Author |
---|---|---|---|
#8 | insert_block-use-custom-2844631-8.patch | 8.55 KB | kbentham |
#7 | insert_block-use-custom-2844631-7.patch | 8.12 KB | kbentham |
#6 | insert_block-use-custom-2844631-6.patch | 9.59 KB | peter.keppert |
#2 | insert_block-use-custom-2844631-2.patch | 8.86 KB | dhansen |
Comments
Comment #2
dhansen CreditAttribution: dhansen at Sevaa Group commentedComment #3
dhansen CreditAttribution: dhansen at Sevaa Group commentedComment #4
aldovega10 CreditAttribution: aldovega10 commentedis this working on Drupal 8.2? How to apply this patch, sorry I'm a newbie
Comment #5
dhansen CreditAttribution: dhansen at Sevaa Group commented@aldovega10 I've got this patch running in Drupal 8.3.1, and it was previously working on 8.2 so it should be fine. You can find some documentation on applying patches here: Applying patches with Git. This patch would work in place of the patch from the other issue you commented on.
Comment #6
peter.keppert CreditAttribution: peter.keppert commentedHi @dhansen, I was experimenting with this patch on a multilingual site. I have two language versions of the inserted custom block. When trying with site in non-default language, the body of the block was displayed correctly, but the title was not translated. I investigated what might be the reasons and my conclusion is, that you are generating the block label from the 'info' property of the custom block entity, which is being retrieved non-translated. I made one small correction to fix this. Updated patch attached. Changing status to Needs review as I believe that fits the situation better.
However I have doubts about doing it this way in the first place. I mean it works, but custom blocks are not intended to be rendered on their own. Good explanation can be found in the following issue: #2704331-19: Ability to display block_content entities independently, also outside of Blocks. After a long deliberation, I ended up creating a separate project named Box, which embeds entities instead of blocks (full list of reasons explained on the project's page).
Comment #7
kbentham CreditAttribution: kbentham at Palantir.net for Tableau commentedI have update the patch to use the latest patch from #2648496. I also reworked the functionality so content in the entity fields are rendered and we aren't building entities and render arrays by hand.
Comment #8
kbentham CreditAttribution: kbentham at Palantir.net for Tableau commentedI compared the branches the wrong way round, here is the updated patch.