I created a block plugin that adds the ability to specify style settings by adding a form for configuring the styles.

Is it possible to extend the block class so that all blocks have this capability and not just the specific block plugin I created. Or is there a hook that can perform this action?


In general, you would use hook_form_alter() or hook_form_FORM_ID_alter() in a module to change all forms of a broad type. For all blocks, it looks like the form ID is always "block_form". So you can easily add elements to all block forms by implementing this function assuming your module is named "MYMODULE"

MYMODULE_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) 

Since most blocks are extending BlockBase there is no way to change the class on the fly.

A really heavy-handed alternative to the form alter would be to use the decorator pattern and change all block plugins via the alter hook in discovery to use your class that adds some form elements and delegates most method calls back to an instance of the original class.


The Block Class module allows to add classes to every block.

To that, it uses a form alter and the third party settings support of config entities. The classes are then added through a template preproprocess.

The details can be found in the source code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.