Field as Block is a lightweight module that allows you to display a field as a block. The same results can be achieved by using Panels, Display Suite or custom code but this module offers a lightweight alternative.
CCK Blocks offers similar functionality, however on the project page they recommend that you use Field as Block for new projects. It looks like CCK Blocks will be deprecated in favour of Field as Block. For more details read issue #1920636 (comment #4).
In this tutorial we’ll use the module to display a “Call to action” field as a block in the sidebar second region.
Getting Started
Before we begin just download and enable the Field as Block module.
If you use Drush, run the following command:
drush dl fieldblock drush en fieldblock
Create Call To Action Field
First we’ll need to create a long text field called “Call to action” on the Article content type.
1. Go to Structure -> “Content types” and click on “manage fields” in the Article row.
2. Create a “Call to action” field using the values defined in Table 1.1.
Table 1-1. Create long text field
Option | Value |
---|---|
Label | Call to action |
Machine name | field_call_to_action |
Field type | Long text |
Widget | Text area (multiple rows) |
Display Field As Block
Now that we have our “Call to action” field the last bit of work we need to do is configure the field to be displayed as a block, and then display that block in a region.
1. Go to Structure -> “Content types” and click on “manage display” in the Article row.
2. In the Default view mode select the “Display as block” checkbox in the “Call to action” row and click on Save.
3. Go to Structure -> Blocks and assign the “Call to action field (from node: Article: default)” to “Sidebar second” region.
Test Block
Now that everything has been setup, let’s go and test the field and make sure it displays as a block. First, go and create an article and add some text into the “Call to action” field.
Once you have saved the form, the text should appear in the “Call to action” block within the “Sidebar second” region.
If you move the “Call to action” formatter to the Hidden area on the “manage displays” page, the block will not appear, even though you have ticked the checkbox.
Make sure the formatter is always enabled and not hidden.
If you have any questions, please leave a comment.
This looks like a pretty neat module for lightweight use cases. Alternatively, you can use the “Region to Block” functionality with Display Suite Extras, if you’re already using Display Suite.
You basically create a region in the display that turns into a block, so any fields you place in that region show up in the block. Works great if you need more than one field in the block.
Hey thanks for the tip. 🙂
Thank you for writing this tutorial. It’s great to see that my little module is actually being used.
Kyle is completely right in his comment. Field as Block is meant to be simple and lightweight. If you need lots of options, different layouts, custom fields etc., I would highly recommend Display Suite.
dont work in drupal 8.2.4
That’s correct, this tutorial is for Drupal 7.
It was written in 2013.
There’s a Drupal 8 version now, it’s alpha though.
Thanks
I’m new to Drupal as of Drupal 8 – actually as of approximately a week ago – but I’m wondering… if I wanted a similar block to the call of action, would I not just create a custom block titled Call to Action and enter my text into the body and place the block wherever on the site I want it?
Hi Christian,
Yes you could do that.
But what if you want to display a different CTA depending on which article or page is being viewed?
This is where “Field As Block” can help and the content of the CTA block can be managed from the article or page edit form.
You shouldn’t give editors access to the “Block layouts” because they could do some major damage to the site by removing blocks from regions and so on.
Cheers,
Ivan
I probably should just go test this, but would it work if the field was a paragraph field? Lets say I wanted to create an advertising banner to display along side the content of that particular node, could I create paragraph field containing multiple fields and set it to display in a block region for my adverts? I suppose you could use if for things like sliders and carousels as well if this works.
Yes you could use Paragraphs in that instance.