You are here

function regcode_tokens in Registration codes 8

Same name and namespace in other branches
  1. 7.2 regcode.module \regcode_tokens()
  2. 7 regcode.module \regcode_tokens()

Implements hook_tokens().

File

./regcode.tokens.inc, line 79
Token support for the regcode module.

Code

function regcode_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  if (isset($options['language'])) {
    $language_code = $options['language']->language;
  }
  else {
    $language_code = NULL;
  }
  $replacements = [];
  if ($type == 'regcode' && !empty($data['regcode'])) {
    $regcode = $data['regcode'];
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'created':
          $replacements[$original] = \Drupal::service('date.formatter')
            ->format($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'lastused':
          $replacements[$original] = \Drupal::service('date.formatter')
            ->format($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'begins':
          $replacements[$original] = \Drupal::service('date.formatter')
            ->format($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'expires':
          $replacements[$original] = \Drupal::service('date.formatter')
            ->format($regcode->created, 'medium', '', NULL, $language_code);
          break;
        case 'code':
          $replacements[$original] = $regcode->code;
          break;
        case 'regurl':
          $replacements[$original] = Url::fromRoute('user.register');
          break;
        case 'is_active':
          $replacements[$original] = $regcode->is_active ? t('active') : t('inactive');
          break;
        case 'maxuses':
          $replacements[$original] = $regcode->maxuses;
          break;
        case 'uses':
          $replacements[$original] = $regcode->uses;
          break;
      }
    }
    if ($created_tokens = \Drupal::token()
      ->findWithPrefix($tokens, 'created')) {
      $replacements += \Drupal::token()
        ->generate('date', $created_tokens, [
        'date' => $regcode->created,
      ], $options, $bubbleable_metadata);
    }
    if ($lastused_tokens = \Drupal::token()
      ->findWithPrefix($tokens, 'lastused')) {
      $replacements += \Drupal::token()
        ->generate('date', $lastused_tokens, [
        'date' => $regcode->lastused,
      ], $options, $bubbleable_metadata);
    }
    if ($begins_tokens = \Drupal::token()
      ->findWithPrefix($tokens, 'begins')) {
      $replacements += \Drupal::token()
        ->generate('date', $begins_tokens, [
        'date' => $regcode->begins,
      ], $options, $bubbleable_metadata);
    }
    if ($expires_tokens = \Drupal::token()
      ->findWithPrefix($tokens, 'expires')) {
      $replacements += \Drupal::token()
        ->generate('date', $expires_tokens, [
        'date' => $regcode->expires,
      ], $options, $bubbleable_metadata);
    }
  }
  return $replacements;
}