You are here

function template_preprocess_group in Group 8

Same name and namespace in other branches
  1. 2.0.x group.module \template_preprocess_group()

Prepares variables for the group template.

Default template: group.html.twig

Parameters

array $variables:

  • elements: An array of elements to display in view mode.
  • group: The group object.
  • view_mode: View mode; e.g., 'full', 'teaser', etc.

File

./group.module, line 118
Allows you to group users, content and other entities.

Code

function template_preprocess_group(&$variables) {

  /** @var \Drupal\group\Entity\GroupInterface $group */
  $group = $variables['elements']['#group'];
  $variables['group'] = $group;
  $variables['view_mode'] = $variables['elements']['#view_mode'];
  $variables['label'] = $group
    ->label();
  $variables['url'] = $group
    ->toUrl('canonical', [
    'language' => $group
      ->language(),
  ]);
  $variables['attributes']['class'][] = 'group';
  $variables['attributes']['class'][] = Html::cleanCssIdentifier('group--' . $variables['view_mode']);
  $variables['attributes']['class'][] = Html::cleanCssIdentifier('group--' . $variables['group']
    ->bundle());

  // See if we are rendering the group at its canonical route.
  $route_match = \Drupal::routeMatch();
  if ($route_match
    ->getRouteName() == 'entity.group.canonical') {
    $page_group = $route_match
      ->getParameter('group');
  }
  $is_page = !empty($page_group) ? $page_group
    ->id() == $group
    ->id() : FALSE;
  $variables['page'] = $variables['view_mode'] == 'full' && $is_page;

  // Helpful $content variable for templates.
  $variables += [
    'content' => [],
  ];
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
}