Need some help with your project? Contact me

Redirecting the block configure page back to itself

After working with Drupal for a while, you begin to love some of its features to death. But you can also develop some pet-peeves. One of mine is how when you edit a block in the administration and save it, you are redirected onto the block overview page - you know, the one with the big list of blocks. Now think you are on a site with a bunch of regions and blocks and you are fine tuning a particular one while testing how it outputs. Every time you have to go back and search for it to edit it again.

I decided to write a little custom code to change that default behaviour more to my liking. In this article we will look at how you can redirect the user back to the block edit page after the block has been saved. Basically how to redirect to itself so you can edit again immediately after you saved the block. It's quite easy actually, it only takes 2 functions:

function module_name_form_block_admin_configure_alter(&$form, &$form_state) {
  $form['#submit'][] = 'module_name_form_block_admin_configure_submit';
}
function module_name_form_block_admin_configure_submit($form, &$form_state) {
  $module = $form['module']['#value'];
  $delta = $form['delta']['#value'];
  $form_state['redirect'] = 'admin/structure/block/manage/' . $module . '/' . $delta . '/configure';
}

Let's see what these do. The first one is an implementation of hook_form_FORM_ID_alter() and we use it to hook into the block edit form and include a custom submit callback to the right array. You can read more about how to do these kind of things here. The value we included is the name of the second function above that gets called after the default block submit handler.

What happens in the second function? First off, we gather the name of the module that created the block. For instance the Search form block is created by the Search module and the Navigation block by the System module (these are default blocks that come with Drupal core in case you were wondering). For custom blocks made in the UI, the name will be block.

Then, we gather the delta value. This is the unique identifier of the block (integer for custom blocks or strings for modules implementing blocks in code). Finally, we create a redirect to the specific URL which includes both these values (i.e., back to the edit form for that block).

Easy peasy, just remember to replace module_name with the machine name of your block.

Now when you have to keep editing a particular block, you won't have to search through all those names to find it back again and again. You are right there in the zone making changes. I think it's also good for content editor experience, not just administrators.

Hope this helps.

Some related articles you might enjoy

Add new comment

You must have Javascript enabled to use this form.