Skip to content

Instantly share code, notes, and snippets.

@rosemarydotworld
Created March 1, 2012 00:13
Show Gist options
  • Save rosemarydotworld/1945947 to your computer and use it in GitHub Desktop.
Save rosemarydotworld/1945947 to your computer and use it in GitHub Desktop.
<?php
//give blocks semantic html ids and theme hook suggestions
function wmux_preprocess_block(&$vars) {
$semantic_block_id = wmux_id_safe($vars['block']->subject);
$vars['semantic_block_id'] = $semantic_block_id;
$vars['theme_hook_suggestions'][] = 'block__' . $semantic_block_id;
}
function wmux_id_safe($string) {
if (is_numeric($string)) {
$string = 'n'. $string;
}
return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '-', $string));
}
@rosemarydotworld
Copy link
Author

The idea is that all blocks become themeable with a template file based on their title:

block--[block-title].tpl.php

Using devel_themer, I can confirm that the theme_hook_suggestions array shows my new value. However, blocks don't actually take the new template when their titles mean they ought to. Is it possible that I'm adding to the theme_hook_suggestions array after the block already decides upon a template?

@rosemarydotworld
Copy link
Author

On the advice of David Gibb, tried the following without success:

$vars['theme_hook_suggestion'] = 'block__' . $semantic_block_id;

@rosemarydotworld
Copy link
Author

Solved by Sam Richard. Here's what I should have been doing on line 14:

return strtolower(preg_replace('/[^a-zA-Z0-9-]+/', '_', $string));

Notice the underscore in the place of the dash.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment