Adding helpful classes and links top Drupal blocks via preprocessing: first, last, odd, even, edit, configure

Drupal's ability to preprocess HTML code in your theme's template.php file is extremely helpful for theme coding and site administration.You can find excellent preprocessing scripts on Drupal.org's forums, or simply use one of the many free Starter Theme such as Zen or Basic. I personally use the Basic Theme, which does quite a lot of theming work for you.

As great as Basic theme's template.php file is, there's still room for improvement. I've added four major improvements to it here:

  1. Integrated with Menu Admin per Menu module, so that the "Edit Menu" and "Configure" links for each block can be controlled per menu & by role.
  2. Integrated with Nice Menus and Menu Block modules.
  3. Integrated with Block Access module, so that the "Edit" and "Configure" links for each block can be controlled per block & by role.
  4. Added first and last classes to blocks per region.

Here is the block precessing portion of my template.php file, Enjoy! $block) { $block->extraclass = ''; $block->extraclass .= ( $blockcounter == 1 ? ' block-first' : '' ); $block->extraclass .= ( $blockcounter == count($list) ? ' block-last' : '' ); $output .= theme('block', $block); $blockcounter++; } } // Add any content assigned to this region through drupal_set_content() calls. $output .= drupal_get_content($region); return $output; } function grasmash_preprocess_block(&$vars, $hook) { $block = $vars['block']; // special block classes $classes = array('block'); $classes[] = grasmash_id_safe('block-' . $vars['block']->module); $classes[] = grasmash_id_safe('block-' . $vars['block']->region); $classes[] = grasmash_id_safe('block-id-' . $vars['block']->bid); $classes[] = 'clearfix'; // support for Skinr Module if (module_exists('skinr')) { $classes[] = $vars['skinr']; } //Add first, last classes generated in theme_blocks $classes[] = $block->extraclass; $vars['block_classes'] = implode(' ', $classes); // Concatenate with spaces if (theme_get_setting('grasmash_block_editing') && (user_access('administer blocks') || (module_exists('block_access') && block_access_can_config($block->module, $block->delta)))) { // Display 'edit block' for custom blocks. if ($block->module == 'block') { $edit_links[] = l('' . t('edit block') . '', 'admin/build/block/configure/' . $block->module . '/' . $block->delta, array( 'attributes' => array( 'title' => t('edit the content of this block'), 'class' => 'block-edit', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); } // Display 'edit menu' for Menu blocks. if ($block->module == 'menu' || ($block->module == 'user' && $block->delta == 1)){ //prep for menu_admin_per_menu module $menu_name = ($block->module == 'user') ? 'navigation' : $block->delta; $menu['menu_name'] = $menu_name; //check user access to menu if (user_access('administer menu') || (module_exists('menu_admin_per_menu') && _menu_admin_per_menu_menu_access($menu))) { $edit_links[] = l('' . t('edit menu') . '', 'admin/build/menu-customize/' . $menu_name, array( 'attributes' => array( 'title' => t('edit the menu that defines this block'), 'class' => 'block-edit-menu', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); } } // Display 'edit menu' for Menu block blocks. elseif ($block->module == 'menu_block'){ //prep for menu_admin_per_menu module list($menu_name, ) = split(':', variable_get("menu_block_{$block->delta}_parent", 'navigation:0')); $menu['menu_name'] = str_replace('_','-',$menu_name); //check user access to menu if (user_access('administer menu') || (module_exists('menu_admin_per_menu') && _menu_admin_per_menu_menu_access($block->delta))) { $edit_links[] = l('' . t('edit menu') . '', 'admin/build/menu-customize/' . $menu_name, array( 'attributes' => array( 'title' => t('edit the menu that defines this block'), 'class' => 'block-edit-menu', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); } } // Display 'edit menu' for Nice Menu blocks. elseif ($block->module == 'nice_menus'){ //prep for menu_admin_per_menu module list($menu_name, ) = split(':', variable_get("nice_menus_menu_{$block->delta}", 'navigation:0')); $menu['menu_name'] = str_replace('_','-',$menu_name); //check user access to menu if (user_access('administer menu') || (module_exists('menu_admin_per_menu') && _menu_admin_per_menu_menu_access($menu))) { $edit_links[] = l('' . t('edit menu') . '', 'admin/build/menu-customize/' . $menu_name, array( 'attributes' => array( 'title' => t('edit the menu that defines this block'), 'class' => 'block-edit-menu', ), 'query' => drupal_get_destination(), 'html' => TRUE, ) ); } } $vars['edit_links_array'] = $edit_links; $vars['edit_links'] = '

' . implode(' ', $edit_links) . '
'; } } ?>

You will need to replace "grasmash" with the name of your theme.

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.