Last active
April 6, 2017 20:36
-
-
Save paulhhowells/4722363 to your computer and use it in GitHub Desktop.
Enable bean blocks to be themed (using a .tpl file) according to bean-type: i.e. block__bean__BEAN-TYPE.tpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Enable bean blocks to be themed (using a .tpl file) according to bean-type: | |
i.e. block__bean__BEAN-TYPE.tpl | |
Why? Combine a Bean Block with Display Suite and the innards of the block will match the DS layout | |
used, however the outer wrapping of the block must be themed separately. However if you write a | |
.tpl that matches the block it will take precedence and the DS layout will be lost. This prevents | |
using a DS layout for each type of Bean while simultaneously having full control over the Bean | |
Block’s mark-up. | |
This solution adds a theme hook suggestion so that .tpl files may be written for each type of Bean. | |
Add this function to template.php | |
<?php | |
/** | |
* Implements template_preprocess_block | |
* | |
* enable bean blocks to be themed by bean-type: | |
* i.e. block__bean__BEAN-TYPE.tpl | |
*/ | |
function THEMENAME_preprocess_block(&$variables) { | |
$block = $variables['block']; | |
$block_module = $block->module; | |
$elements = $variables['elements']; | |
// Add Bean Type to Theme Hook Suggestions | |
// enable bean blocks to be themed by bean-type: i.e. block__bean__BEAN-TYPE.tpl | |
// select Bean Blocks and ignore other Blocks | |
if (!empty($block_module) && ($block_module == 'bean')) { | |
// the location of the Bean array within $variables is a moving target | |
// e.g. use of the Context Module will change it's location to within $elements['content'] | |
// try the likely locations first before recursively iterating through $elements | |
if (array_key_exists('bean', $elements)) { | |
$bean_array = $elements['bean']; | |
} else if (array_key_exists('bean', $elements['content'])) { | |
$bean_array = $elements['content']['bean']; | |
} else { | |
$bean_array = array_find_first_value('bean', $elements); | |
} | |
// test that the Bean array has been found | |
if ($bean_array) { | |
$mystery_key_array = element_children($bean_array); | |
if ($mystery_key_array) { | |
$bean = $bean_array[$mystery_key_array[0]]; | |
if (!empty($bean['#bundle'])) { | |
$variables['theme_hook_suggestions'][] = 'block__' . $block_module . '__' . $bean['#bundle']; | |
} | |
} | |
} | |
} | |
} | |
/* C U S T O M F U N C T I O N S */ | |
/* | |
* search an array recursively to find | |
* the first instance of a key and return its value | |
* | |
* useful when you do not know the location of a key nested (only once) in a multidimensional array | |
* | |
*/ | |
function array_find_first_value($needle_key, array $haystack) { | |
if (array_key_exists($needle_key, $haystack)) { | |
return $haystack[$needle_key]; | |
} | |
foreach ($haystack as $key => $value) { | |
if (is_array($value)) { | |
$result = array_find_first_value($needle_key, $value); | |
if ($result) { | |
return $result; | |
} | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
added use of array_find_first_value() in case the location of the Bean array varies more than I know