Deploy Drupal's blocks on a different instance using Features without having to re-map your CSS

Ryan Weal
Published 2012-09-17

Get the fe_block module as part of Features Extra.

  • drush dl features_extra drush pm-enable fe_block
  • Go to your blocks page.
  • Configure any custom blocks you are planning to deploy.
  • When you edit a custom block you will now have a "machine_name" variable. Give your block a machine name. Now the block is available for Features export.
  • You can now go to Features in your Structure menu and generate a new feature with the custom block(s) and their settings (placement in the theme, if it will be the same theme on the target system). This is great so far, but how will we keep our CSS consistent when the block's ID will change?

There is a block_class module but it does not export data to Features. Sad face... but wait! We have a solution: We can do a custom block template in our theme to add the machine_name as a custom class for this block. So the machine_name for each block will now (1) enable the export and (2) get a class of the same name if the theme supports it. Copy over your block.tpl.php file to your theme's templates folder. If you have this file in your theme already, great! If you're using a starter kit theme, look in the starter kit's templates folder for this file, and copy it to templates/block.tpl.php in your theme.

If you are coding your theme from scratch, grab the original block.tpl.php file from modules/block/block.tpl.php under the root of your site. Now that you have added that file to your theme, using the name templates/block.tpl.php : Find the line that defines the 'class' in HTML. It is probably really close to the top. Put the following line before it. This code will add the machine name as a class while leaving the rest of the code in-place:

`<?php $classes .= ' ' . _fe_block_get_machine_name($block->delta); ?>`

Be sure to test this code before you put it live! Otherwise, get to work on your CSS using the machine_name defined by fe_block module! Enjoy never copying and pasting your blocks ever again!

Thanks for Reading!
Ryan Weal (LinkedIn)
🍪 ☕
Send a comment 💬
Published by Kafei Interactive