Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Coming from using boxes I am used to a way to add classes to boxes blocks. What is the best way to add classes to bean?
Should we use www.dgo.to/block_class?
Comments
Comment #1
rooby CreditAttribution: rooby commentedDo you want to add classes to the bean (in the block content) or the parent block itself?
If the former, you can use template_preprocess_bean(&$variables) and if the latter you can use template_preprocess_block(&$variables) and add classes to the $variables['classes_array'].
See this page for info about preprocess functions: http://drupal.org/node/223430
For example (in your theme's template.php file):
This example adds a custom class to the bean:
This example adds a class to the parent block that contains the bean type and also adds a template suggestion based on the bean type:
Comment #2
rooby CreditAttribution: rooby commentedActually it seems that for some reason template_preprocess_bean() does not work.
Comment #3
rooby CreditAttribution: rooby commentedInstead of template_preprocess_bean() you could do this:
Also, for more themeing chat see #1361756: More theme candidates.
Marking fixed, if you still require more help feel free to reopen.
Comment #5
a-fro CreditAttribution: a-fro commentedrooby's suggestion above got me on the right track. However, I couldn't get my modified classes_array to output to the page with hook_process_entity. I had to use the following, which came from the suggestion #1361756: More theme candidates:
Comment #6
Nicolas Bouteille CreditAttribution: Nicolas Bouteille commentedtemplate_process_entity did not work for me either but template_PREprocess_entity did work and actually helped me add my class where I needed it to be. Because when rendering my entity through views, adding the class via template_preprocess_block does not work.
Comment #7
droweski CreditAttribution: droweski commentedWe include class field(select list) in our beans.
Then use this code to add the class to the entity attributes:
/**
* Runs a entity specific preprocess function, if it exists.
*/
function bean_library_preprocess_entity(&$variables, $hook) {
$function = __FUNCTION__ . '_' . $variables['entity_type'];
if (function_exists($function)) {
$function($variables, $hook);
}
}
/**
* Implementation of template_preprocess_entity().
*/
function bean_library_preprocess_entity_bean(&$variables, $hook) {
if(isset($variables['elements']['#entity']->field_html_bean_class)) {
$newclass = $variables['elements']['#entity']->field_html_bean_class['und'][0]['value'];
$variables['classes_array'][] = $newclass;
}
}
Note that you should change the name of your function to your module name (as usual).
Comment #8
MrPaulDriver CreditAttribution: MrPaulDriver commentedI am new to preprocess functions and curious to know how to insert a token as a class.
Following through with the suggestion at #5. How to insert the block ID (or bean ID) as a class instead of 'your-custom-class' as stated in the examples above.
I think an appropriate token may be [bean:uid]
Comment #9
MrPaulDriver CreditAttribution: MrPaulDriver commentedTemporarily re-opening to aid visability
Comment #10
nicrodgers