You are here

function forward_tokens in Forward 8

Same name and namespace in other branches
  1. 8.3 forward.tokens.inc \forward_tokens()
  2. 8.2 forward.tokens.inc \forward_tokens()
  3. 7.3 forward.tokens.inc \forward_tokens()
  4. 7.2 forward.module \forward_tokens()
  5. 4.x forward.tokens.inc \forward_tokens()
  6. 4.0.x forward.tokens.inc \forward_tokens()

Implements hook_tokens().

File

./forward.tokens.inc, line 53
forward.tokens.inc Provide tokens related to the forward module

Code

function forward_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $replacements = array();
  if ($type == 'forward' && !empty($data['forward'])) {
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'entity-type':
          if (isset($data['forward']['entity'])) {
            $entity = $data['forward']['entity'];
            $entity_type = (string) $entity
              ->getEntityType()
              ->getLabel();
            if ($entity
              ->getEntityType()
              ->hasKey('bundle')) {

              // For entity types with bundles, use the bundle label instead of the entity type label
              $bundles = \Drupal::service('entity_type.bundle.info')
                ->getBundleInfo($entity
                ->getEntityTypeId());
              $entity_type = $bundles[$entity
                ->bundle()]['label'];
            }

            // Use the translated label, then lowercase it
            $entity_type = strtolower(t($entity_type, array(), $options));
          }
          $replacements[$original] = isset($data['forward']['entity']) ? Markup::create(Xss::filter($entity_type)) : t('page');
          break;
        case 'entity-title':
          if (isset($data['forward']['entity'])) {
            $entity = $data['forward']['entity'];
            $entity_title = $entity
              ->access('view') ? $entity
              ->label() : t('a page');
          }
          $replacements[$original] = isset($data['forward']['entity']) ? Markup::create(Xss::filter($entity_title)) : t('a page');
          break;
        case 'recipients':
          $replacements[$original] = isset($data['forward']['recipients']) ? Markup::create(Xss::filter(implode($data['forward']['recipients'], ', '))) : t('(Recipient list)');
          break;
        case 'sender-name':
          $replacements[$original] = isset($data['forward']['sender_name']) ? Markup::create(Xss::filter($data['forward']['sender_name'])) : t('(Your name)');
          break;
        case 'sender-email':
          $replacements[$original] = isset($data['forward']['sender_email']) ? Markup::create(Xss::filter($data['forward']['sender_email'])) : t('(Your email address)');
          break;
      }
    }
  }
  return $replacements;
}