Both WordPress and Drupal have long had the ability to place arbitrary content throughout your site. WordPress calls them Widgets, while Drupal has named them Blocks. Beyond the name they essentially provide the same functionality, but previous to Drupal 8, blocks have lacked the ability to be placed on a page more than once.

Not to go into a deep Drupal history lesson here or anything, but TL;DR, Drupal blocks in the past have been: 1 API implementation = 1 block. If you created a block in code, that block could be used exactly once in any given block placement interface. *

As a long time widget/block fan and WordPress/Drupal developer, I was naturally very excited when Drupal 8 adopted an instance approach to blocks that finally matches the flexibility of WordPress’s implementation.

Let’s take a look at how similar they are now

WordPress Widget Example

Widget Form

Drupal 8 Block Example

Block Form

That’s pretty darn similar!

Summary

Both classes now do the same following things, with the remaining differences primarily being class/method signatures or other internal APIs (Drupal).

Similarities

  • Both systems use inheritance to ease the development of a new subtype.
    WordPress widgets extend the WP_Widget class
    Drupal blocks extend BlockBase
  • Both provide a means of overriding the output of the parent class.
    WordPress calls this method widget()
    Drupal calls it build()
  • Both provide a means of overriding the configuration form of the parent class.
    WordPress calls this method form()
    Drupal calls it blockForm()
  • Both provide a means of saving configuration form values.
    WordPress calls it update()
    Drupal calls it blockSubmit()
  • Both save the configuration to the database on their own.

Differences

  • Drupal requires the use of many more internal APIs not specifically related to the Block API.
  • Drupal’s build() method must return a render array.
    WordPress expects the widget to echo its contents.
  • Drupal’s blockForm() method must return a Form API array.
    WordPress expects the widget to echo the form immediately.
  • On submission of the configuration form:
    Drupal needs you to set the new values within the block’s configuration array
    WordPress expects you to return an array with the new values

That’s not so different!

About the Author

Jonathan Daggerhart

Long time Drupal and WordPress developer. I like to write modules and plugins, and I dabble in frontend and design.

Leave a Reply

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