I created a template for the group overview page but could not hook into preprocess in my custom module. So, I added it to the group module. This allows other modules to create hook_preprocess_group_bundle_view functions so that variables can be sent to custom templates.

CommentFileSizeAuthor
templatepreprocess.patch862 bytesbletch
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bletch created an issue. See original summary.

kristiaanvandeneynde’s picture

I think Entity API provides those preprocess functions for us, no?

Like hook_preprocess_entity() and I think even hook_preprocess_group?

bletch’s picture

I could not get hook_preprocess_entity to fire in my custom module. I had to add the hook into the group module to trigger the hook_preprocess_group in my custom module. Maybe something else was going on with my setup but it was the only way I could get those preprocess hooks to fire.

kristiaanvandeneynde’s picture

Category: Feature request » Support request
Status: Active » Fixed

Right, what you need is hook_preprocess_entity(). Then check if $variables['entity_type'] == 'group'

Status: Fixed » Closed (fixed)

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

isaachorton’s picture

is any way someone can post a code example of how this would be implemented, Im a designer trying to style a theme for the group module and I cant figure out how to get $group variables into the templates? Cant seem to find a code example anywhere. ive searched hi and lo. Specifically, i want to render an image field from the group in the page--group.tpl.php but outside of the content, just above the title. I can access the variable in the devel module and target the field that way, but then the id of the group is passed and so will only load 1 groups image, but the goal is to load a header image for each group that is unique to the group and comes from a field of the group, ie an image field.

I know it's probably really easy for someone, but for me this is nearly impossible to figure out. Any help would be greatly appreciated!

kristiaanvandeneynde’s picture

Look at template_preprocess_entity()

/**
 * Process variables for entity.tpl.php.
 */
function template_preprocess_entity(&$variables) {
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $entity_type = $variables['elements']['#entity_type'];
  $variables['entity_type'] = $entity_type;
  $entity = $variables['elements']['#entity'];
  // THIS LINE ALREADY SETS A $group VARIABLE
  $variables[$variables['elements']['#entity_type']] = $entity;

  // Snip

  // THESE LINES ADD group.tpl.php, group--type.tpl.php, ETC. AS SUGGESTIONS
  // Add suggestions.
  $variables['theme_hook_suggestions'][] = $entity_type;
  $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle;
  $variables['theme_hook_suggestions'][] = $entity_type . '__' . $bundle . '__' . $variables['view_mode'];
  if ($id = entity_id($entity_type, $entity)) {
    $variables['theme_hook_suggestions'][] = $entity_type . '__' . $id;
  }
}

It should work out of the box

isaachorton’s picture

Thanks for the example Kristiaan!
I added the code you provided above to my custom module and it seems to be creating the template suggestions but my snippet of code I usually use for inserting fields(on page.tpl.php templates) from nodes is not working. How can I alter this code to target the group entity type since the node variable is not available? my field is "field_header_image". thanks again.

    $field = field_view_field('node', $node, 'field_header_image', array('label'=>'hidden'));
     print render($field);
    
kristiaanvandeneynde’s picture

You shouldn't copy any of the code above (#7), Entity API runs that for you for any custom entity type (group, commerce product, ...).

What I was saying is that you should have a $group variable in group.tpl.php out of the box, just as you should have a $node variable in node.tpl.php out of the box because the node module takes care of that in template_preprocess_node().

Maybe you're getting confused by the theming layer a little bit :) This link has some documentation on preprocessors: https://www.drupal.org/node/223430

P.S.: Even though you shouldn't call field_view_field() in a template, it would look like this:

$field = field_view_field('group', $group, 'field_X', $options);
isaachorton’s picture

cool thanks