1

I have written a Drupal 7 module that adds about ten fields to the block administration configuration form and saves the values for those fields in its own database table. My module does not create blocks, but rather extends the functionality of any block, regardless of what module created it.

My module should delete the data in its own table when any module deletes a block from the block table. However, how would my module know when this happens? Is there a hook or other "universal" method available to know when a block has been deleted?

There isn't a hook_block_delete(), even though there are hook_node_delete() and hook_entity_delete().
I've looked at development guides about creating blocks for some clues, but none of the ones I've found give examples of how they should be deleted. I've also looked at the source code of modules that create/delete blocks, like the Views module, but I can't tell how, or if, they send a notice that they have deleted a block.

1

There is no hook fired in delete of block, as you can see in the list of block hooks here block.api.php.

There is a topic in Drupal forum here.

I think the best way to handle this is by hook_form_alter something like.

/**
 * Implements hook_form_alter().
 */
function YourModule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'block_custom_block_delete') {
    // You get all information here of the concerned block in $form.
  }
}
| improve this answer | |
  • I saw that topic, but block_custom_block_delete is the form_id for deleting a block created by the Block module. The ones created by going to Admin >> Structure >> Blocks >> Add Block. It isn't used by other modules when they are deleting their own blocks. – runswithscissors Nov 10 '19 at 21:31
  • 2
    Blocks created by modules using hook_block_info and hook_block_view not deletable. – berramou Nov 10 '19 at 21:45
  • 1
    Some modules may allow you to create and delete their blocks (Views for example) but as @berramou implies there is no general way to know when this happens. What you might do is to use the approach from drupal.stackexchange.com/questions/73676/… in a hook_cron to periodically check that there are no redundant entries in your db table. – Alfred Armstrong Nov 11 '19 at 11:27
  • Thank you @alfred-armstrong for confirming that there isn't a universal / general way to know when a contributed module deletes its block from the block table. I was hoping to avoid hook_cron(); seemed a waste to constantly check for something that would rarely happen. However, your link gave me an idea. My module alters block_admin_display_form(), Block's form for displaying all of the blocks on the site. I should be able to use it to check if there are blocks in my table that are no longer in $form['blocks'], and remove them. – runswithscissors Nov 12 '19 at 5:44

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.