You are here

function rng_tokens in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 rng.tokens.inc \rng_tokens()
  2. 3.x rng.tokens.inc \rng_tokens()

Implements hook_tokens().

File

./rng.tokens.inc, line 57
Builds tokens for RNG.

Code

function rng_tokens($type, $tokens, array $data = [], array $options = []) {
  $replacements = [];
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = NULL;
  }
  if ($type == 'registration' && !empty($data['registration'])) {
    if (($registration = $data['registration']) && $registration instanceof RegistrationInterface) {
      foreach ($tokens as $name => $original) {
        switch ($name) {
          case 'id':
            $replacements[$original] = $registration
              ->id() ?: t('Unassigned');
            break;
          case 'created':
            $replacements[$original] = $registration
              ->id() ? format_date($registration
              ->getCreatedTime(), 'medium', '', NULL, $langcode) : t('Unassigned');
            break;
          case 'changed':
            $replacements[$original] = $registration
              ->id() ? format_date($registration
              ->getChangedTime(), 'medium', '', NULL, $langcode) : t('Unassigned');
            break;
          case 'url':
            $replacements[$original] = $registration
              ->id() ? $registration
              ->url('canonical', $url_options) : t('Unassigned');
            break;
          case 'edit-url':
            $replacements[$original] = $registration
              ->id() ? $registration
              ->url('edit-form', $url_options) : t('Unassigned');
            break;
        }
      }
    }
  }
  return $replacements;
}