0

Unfortunately, HTML commands in block titles are not being rendered. I would like to be able to add icon font symbols to my block titles by adding comands like <i class="icon-envelope"></i> (this is Font Awesome syntax) to them WITHOUT installing yet another contrib module.

Is there a way to let HTML in block titles being rendered by adding a mytheme_preprocess_block() function to my template.php?

I came across a solution to let HTML commands in menu links being rendered by adding

function mytheme_link($variables) {
    $variables['options']['html'] = TRUE;
    return '' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '';
}
to my template.php but haven't managed to modify the code for block titles.

2 Answers 2

1

In preprocess_block() function you can only define the title prefix and suffix for each title.

For example:

/**
 * Override or insert variables into the block template.
 */
function YOURTHEME_preprocess_block(&$variables) {

  $variables['title_prefix'] = array(
    '#type' => 'markup',
    '#markup' => '<i class="icon-envelope">',
  );
  $variables['title_suffix'] = array(
    '#type' => 'markup',
    '#markup' => '</i>',
  );

}
4
  • I would like to use on each block a different icon. It would be easier if I could just insert the icon code into the block title field and have it being rendered. Is this not possible?
    – deinqwertz
    Commented May 27, 2013 at 21:45
  • Just block titles. (Did I get your question right?)
    – deinqwertz
    Commented May 27, 2013 at 22:08
  • Thank you, this partially helps. But isn't there a way to tell Drupal to render all HTML commands that appear in block titles that I entered through the UI? It'd be much easier if I could just change the font icon through the UI (change the block title) instead of modifying template.php every time I want to change the icon.
    – deinqwertz
    Commented May 27, 2013 at 22:31
  • Maybe this module will meet your needs drupal.org/project/block_class_styles
    – Jack-PL
    Commented May 27, 2013 at 23:13
0

In some themes, such as Zen, The block title gets turned into the block "subject" (a variable that is ran through Drupal check_plain() function) in the process function. You want to override the process function, and NOT the preprocess function.

function mytheme_process_block(&$variables, $hook) {
  $block = $variables['block'];
  if (isset($block->title) && strlen($block->title) > 0) {
    $variables['title'] = $block->title;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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