You are here

function webform_access_tokens in Webform 6.x

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

Implements hook_tokens().

File

modules/webform_access/webform_access.tokens.inc, line 78
Builds placeholder replacement tokens for webform access type.

Code

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

    /** @var \Drupal\webform_access\WebformAccessGroupStorageInterface $webform_access_group_storage */
    $webform_access_group_storage = \Drupal::entityTypeManager()
      ->getStorage('webform_access_group');

    /** @var \Drupal\webform\WebformSubmissionInterface $webform_submission */
    $webform_submission = $data['webform_access'];
    $webform = $webform_submission
      ->getWebform();
    $source_entity = $webform_submission
      ->getSourceEntity();
    foreach ($tokens as $name => $original) {

      // $name => [webform_access:$type:$webform_access_type_id:$property].
      $parts = explode(':', $name);

      // Type is always defined (type, admins, users, or emails)
      $type = $parts[0];

      // Access type id is optional.
      $webform_access_type_id = isset($parts[1]) ? $parts[1] : NULL;

      // Properties can be admins, users, or emails.
      $property = isset($parts[2]) ? $parts[2] : NULL;

      /** @var \Drupal\webform_access\WebformAccessGroupInterface $webform_access_group */
      $webform_access_groups = $webform_access_group_storage
        ->loadByEntities($webform, $source_entity, NULL, $webform_access_type_id);
      $webform_access_group_ids = array_keys($webform_access_groups);
      if ($webform_access_groups) {
        $emails = [];

        // Get access group administrator email addresses.
        // Administrator email addresss are only returned if explicitly
        // requested via [webform_access:type:admins] or
        // [webform_access:type:TYPE_ID:admins].
        if (in_array($type, [
          'admins',
          'all',
        ]) || in_array($property, [
          'admins',
          'all',
        ])) {
          $emails = array_merge($emails, _webform_access_tokens_get_access_group_emails('admin', $webform_access_group_ids));
        }

        // Get access group user email addresses.
        // [webform_access:users]
        // [webform_access:type:TYPE_ID]
        // [webform_access:type:TYPE_ID:users]
        if (in_array($type, [
          'users',
          'all',
        ]) || $type === 'type' && (empty($property) || in_array($property, [
          'users',
          'all',
        ]))) {
          $emails = array_merge($emails, _webform_access_tokens_get_access_group_emails('user', $webform_access_group_ids));
        }

        // Get access group 'custom' email addresses.
        // [webform_access:emails]
        // [webform_access:type:TYPE_ID]
        // [webform_access:type:TYPE_ID:emails]
        if (in_array($type, [
          'emails',
          'all',
        ]) || $type === 'type' && (empty($property) || in_array($property, [
          'emails',
          'all',
        ]))) {
          foreach ($webform_access_groups as $webform_access_group) {
            if ($webform_access_group_emails = $webform_access_group
              ->getEmails()) {
              $emails = array_merge($emails, $webform_access_group_emails);
            }
          }
        }
        $emails = array_unique($emails);
        $replacements[$original] = implode(',', $emails);
      }
    }
  }
  return $replacements;
}