• Home Using Fieldable Blocks in Drupal 8

Using Fieldable Blocks in Drupal 8

OMG! Fieldable blocks in core!

“Dan, dub tee eff are you talking about?”

Let’s back up: One of the tools I use to build sites and applications is Drupal. Drupal can be very powerful, but has it’s drawbacks. One of the major drawbacks of Drupal 7 was the built in layout system. It was essentially a list of regions in which you could place blocks of generic content or blocks provided by other modules.

This was handy, but the limitations were many. Let’s say you wanted a block on one page in the header and in the footer on another page. This was not easily done without duplication. Another big issue was that generic blocks pretty much only contained text. If you wanted a block on one page to have an image and the same block on a different page to have a different issue, then you had to duplicate the block. This made the usefulness of having centralized content less useful.

But now? You can put the same block in multiple locations! You can also add fields. This means that you can create a Block Type, which is just like a normal Content Type, except it lives in blocks instead of pages. Oh, the flexibility!

“Ok, so… I read to here and I still don’t understand why I you are excited.”

Let’s give some examples:

Hero sections are very common. They are essentially a big billboard on your site. Often, the request is to have different images on each page. The way I would have handled this in the past is to create a Hero Content Type, then add an image field, then probably a location field, then build a view with a block that took the location as a Contextual Filter/Argument that then selected the right piece of content. Boom – hero. Easy right?

Here’s how I do it now: Create a new Hero Block Type, add an image field, place the blocks. and add the content. Done.

Here’s another good example. I wanted to be able to control the bootstrap columns from the admin interface. Obviously with Drupal 8 being so new, I didn’t have any fancy contributed modules to do it for me. So I added a few fields like Small Column, Med Column, and Large Column, than added some logic in the Twig file (Love Twig!) to add the appropriate classes based on the selection in the block admin UI.

I’m now working with a designed on a site and anytime she asks for a change to something that doesn’t make sense as a rule and is fairly arbitrary to me, I simply add it as a field with options. “Can I get this title italicized?” Add a Italic_Title field with a toggle, turn the power back to your designer, and you’re done! they’ll never have to ask for another italicized entry again.

Go forth and field your blocks!