Wouldn’t it be nice if you could add any block you want to your paragraphs?
In years past, layout for Drupal has been in the hands of front-end developers, but over time various modules were developed that provided site-builders the ability to adjust the layout. An improvement yes, but there still wasn’t a clear cut option that empowered content editors to alter the layout during the editorial process.
Look out! Here comes the Paragraphs Module. This module has been taking the Drupal community over by storm because it allows content editors to add pre-designed components which gives each page the option to have different layouts. One of the limitations of the Paragraphs module, is that each paragraph can only be used once, and only for the current node you are editing. This means that you can’t re-use a common paragraph such as a call to action block, email signup or contact us form, so you end up finding yourself duplicating a lot of work if you want the same block on numerous pages. While the Drupal community has been working to help solve this problem by allowing the re-use of paragraphs, there are still going to be plenty of situations where you want to insert custom blocks, views, or system blocks such as the site logo or login block.
How do you allow your site editors to add re-used blocks into their content during the editorial process?
Let me introduce you to the Block Field Module. Maintained by the one and only Jacob Rockowitz (you know the webform guy ), you can be assured that the code follows best practices and that there will be support. The block field module allows you to reference any block regardless of where it is coming from and the best part, you don’t have to create some hidden region in your theme in order for the blocks to be rendered.
There are plenty of awesome articles out there that explains how to use paragraphs so I won’t get into that. To follow along with my steps be sure to have downloaded and enabled both the Paragraphs and the Block Field modules.
Steps to Add Blocks to Paragraphs
- Download and Enable the Paragraphs and Block Field modules.
- Create a paragraph type called Block Reference (or whatever name you want)
- Add a new field, by selecting the Block (plugin) field type from the dropdown and save it.
- Go to manage display and make the label hidden.
I always forget this step and then I scratch my head when I see the Block Ref field label above my views title.
- Now go to back to your content type that has the paragraph reference field and ensure the Block Reference paragraph type is correctly enabled.
The content type with the paragraph reference field was not covered in this tutorial.
- When adding or editing your content with a paragraph reference field. Add the Block Reference paragraph type. Select the name of the block that you would like to reference from the dropdown hit save on the content and watch the magic happen.
In conclusion, it does feel a little scary giving content editors this much freedom so it will be imperative that all views and custom blocks have descriptive names so that editors can clearly identify what blocks to reference. Overall I feel like this is a good solution for referencing existing blocks that can save a lot of time and really unleashes the power of the paragraphs module. The Drupal community continues to amaze me!
If you are interested in contributing blog post or want to get more involved with the Atlanta Drupal Users Group (ADUG) please feel free to reach out firstname.lastname@example.org