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

rooby’s picture

Do 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:

<?php
/**
 * Override or insert variables into the bean templates.
 */
function YOURTHEME_preprocess_bean(&$variables) {
  // Add a custom class to the bean entity.
  $variables['classes_array'][] = 'my-custom-class';
}
?>

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:

<?php
/**
 * Override or insert variables into the block templates.
 */
function YOURTHEME_preprocess_block(&$variables) {
  // For bean blocks.
  if ($variables['block']->module == 'bean') {
    // Get the bean elements.
    $beans = $variables['elements']['bean'];
    // There is only 1 bean per block.
    $bean = $beans[reset(element_children($beans))];
    // Add bean type classes to the parent block.
    $variables['classes_array'][] = drupal_html_class('block-bean-' . $bean['#bundle']);
    // Add template suggestions for bean types.
    $variables['theme_hook_suggestions'][] = 'block__bean__' . $bean['#bundle'];
  }
}
?>
rooby’s picture

Actually it seems that for some reason template_preprocess_bean() does not work.

rooby’s picture

Status: Active » Fixed

Instead of template_preprocess_bean() you could do this:

<?php
function YOURTHEME_process_entity(&$variables) {
  if ($variables['entity_type'] == 'bean') {
    if ($variables['bean']->type == 'YOUR_BEAN_TYPE') {
      $variables['classes_array'][] = 'your-custom-class';
    }
  }
}
?>

Also, for more themeing chat see #1361756: More theme candidates.

Marking fixed, if you still require more help feel free to reopen.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

a-fro’s picture

rooby'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:

<?php
function YOURTHEME_preprocess_block(&$variables) {
  if ($variables['block']->module == 'bean') {
      $variables['classes_array'][] = 'your-custom-class';
  }
}
?>
Nicolas Bouteille’s picture

Issue summary: View changes

template_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.

droweski’s picture

We 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).

MrPaulDriver’s picture

I 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]

MrPaulDriver’s picture

Status: Closed (fixed) » Active

Temporarily re-opening to aid visability

nicrodgers’s picture

Status: Active » Closed (works as designed)