You are here

function ggroup_tokens in Subgroup (Graph) 1.0.x

Implements hook_tokens().

File

./ggroup.tokens.inc, line 46
Builds placeholder replacement tokens for group group-related data.

Code

function ggroup_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  if ($type != 'group' || empty($data['group']) || empty($data['group']
    ->id())) {
    return [];
  }
  $token_service = \Drupal::token();
  $replacements = [];
  $group_content_array = GroupContent::loadByEntity($data['group']);
  if (empty($group_content_array)) {
    return [];
  }
  $groups = [];

  /** @var \Drupal\group\Entity\GroupContentInterface $group_content */
  foreach ($group_content_array as $group_content) {
    $group = $group_content
      ->getGroup();
    $groups[$group
      ->id()] = $group
      ->label();
    $bubbleable_metadata
      ->addCacheableDependency($group);
  }
  if (isset($tokens['groups'])) {
    $replacements[$tokens['groups']] = token_render_array($groups, $options);
  }

  // [group:groups:*] chained tokens.
  if ($parents_tokens = $token_service
    ->findWithPrefix($tokens, 'groups')) {
    $replacements += $token_service
      ->generate('array', $parents_tokens, [
      'array' => $groups,
    ], $options, $bubbleable_metadata);
  }

  /** @var \Drupal\group\Entity\GroupContentInterface $group_content */
  $group_content = array_pop($group_content_array);
  $group = $group_content
    ->getGroup();
  if (isset($tokens['group'])) {
    $replacements[$tokens['group']] = $group
      ->label();
  }
  $langcode = $data['group']
    ->language()
    ->getId();
  $group = $group
    ->getTranslation($langcode);
  if ($group_tokens = $token_service
    ->findWithPrefix($tokens, 'group')) {
    $replacements += $token_service
      ->generate('group', $group_tokens, [
      'group' => $group,
    ], $options, $bubbleable_metadata);
  }
  return $replacements;
}