Create A Call To Action Block Using The Field As Block Module

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)

Fig 1.1

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.

Fig 1.2

3. Go to Structure -> Blocks and assign the “Call to action field (from node: Article: default)” to “Sidebar second” region.

Fig 1.3

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.

Fig 1.4

Once you have saved the form, the text should appear in the “Call to action” block within the “Sidebar second” region.

Fig 1.5

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.

Fig 1.6

Make sure the formatter is always enabled and not hidden.

If you have any questions, please leave a comment.

About The Author

11 thoughts on “Create A Call To Action Block Using The Field As Block Module”

  1. 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.

  2. 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.

  3. 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?

    1. 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

  4. Grant Colloty

    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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top