You are here

function webform_group_tokens in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_group/webform_group.tokens.inc \webform_group_tokens()

Implements hook_tokens().

File

modules/webform_group/webform_group.tokens.inc, line 89
Builds placeholder replacement tokens for webform group roles.

Code

function webform_group_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = [];
  if ($type === 'webform_group' && !empty($data['webform_group'])) {

    /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
    $webform_submission = $data['webform_group'];

    /** @var \Drupal\webform_group\WebformGroupManagerInterface $webform_group_manager */
    $webform_group_manager = \Drupal::service('webform_group.manager');
    $group_content = $webform_group_manager
      ->getWebformSubmissionGroupContent($webform_submission);
    if (!$group_content) {
      return $replacements;
    }
    $group = $group_content
      ->getGroup();
    $group_type = $group
      ->getGroupType();
    $group_type_id = $group_type
      ->id();
    foreach ($tokens as $name => $original) {
      $parts = explode(':', $name);
      $token_name = $parts[0];
      if ($token_name === 'role') {

        // Get group role id.
        $token_role_id = isset($parts[1]) ? $parts[1] : NULL;
        if (!$token_role_id) {
          continue;
        }

        // Make sure the group token is allowed.
        if (!$webform_group_manager
          ->isGroupRoleTokenEnabled($token_role_id)) {
          continue;
        }

        // Check if role id is just the role name and prepend the
        // current group's type id.
        $group_roles_ids = $group_type
          ->getRoleIds();
        $group_type_role_id = "{$group_type_id}-{$token_role_id}";
        if (isset($group_roles_ids[$group_type_role_id])) {
          $token_role_id = $group_type_role_id;
        }

        // If token role id is *-member get all members.
        if ($token_role_id === "{$group_type_id}-member") {
          $members = $group
            ->getMembers();
        }
        else {
          $members = $group
            ->getMembers([
            $token_role_id,
          ]);
        }
        $emails = [];
        foreach ($members as $member) {
          $emails[] = $member
            ->getUser()
            ->getEmail();
        }
        $emails = array_unique($emails);
        $replacements[$original] = implode(',', $emails);
      }
    }

    /* Chained token relationships. */
    if ($webform_group_manager
      ->isGroupOwnerTokenEnable()) {
      $token_service = \Drupal::token();
      if ($owner_tokens = $token_service
        ->findWithPrefix($tokens, 'owner')) {
        $replacements += $token_service
          ->generate('user', $owner_tokens, [
          'user' => $group
            ->getOwner(),
        ], $options, $bubbleable_metadata);
      }
    }
  }
  return $replacements;
}