You are here

function log_tokens in Log entity 8

Implements hook_tokens().

File

./log.tokens.inc, line 75
Builds placeholder replacement tokens for log-related data.

Code

function log_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();
  $url_options = array(
    'absolute' => TRUE,
  );
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = LanguageInterface::LANGCODE_DEFAULT;
  }
  $replacements = array();
  if ($type == 'log' && !empty($data['log'])) {

    /** @var \Drupal\log\LogInterface $log */
    $log = $data['log'];
    foreach ($tokens as $name => $original) {
      switch ($name) {

        // Simple key values on the log.
        case 'id':
          $replacements[$original] = $log
            ->id();
          break;
        case 'vid':
          $replacements[$original] = $log
            ->getRevisionId();
          break;
        case 'type':
          $replacements[$original] = $log
            ->getType();
          break;
        case 'type-name':
          $replacements[$original] = $log
            ->getTypeName();
          break;
        case 'title':
          $replacements[$original] = $log
            ->label();
          break;
        case 'langcode':
          $replacements[$original] = $log
            ->language()
            ->getId();
          break;
        case 'url':
          $replacements[$original] = $log
            ->toUrl('canonical', $url_options);
          break;
        case 'edit-url':
          $replacements[$original] = $log
            ->toUrl('edit-form', $url_options);
          break;

        // Default values for the chained tokens handled below.
        case 'author':
          $account = $log
            ->getOwner() ? $log
            ->getOwner() : User::load(0);
          $bubbleable_metadata
            ->addCacheableDependency($account);
          $replacements[$original] = $account
            ->label();
          break;
        case 'created':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata
            ->addCacheableDependency($date_format);
          $replacements[$original] = format_date($log
            ->getCreatedTime(), 'medium', '', NULL, $langcode);
          break;
        case 'changed':
          $date_format = DateFormat::load('medium');
          $bubbleable_metadata
            ->addCacheableDependency($date_format);
          $replacements[$original] = format_date($log
            ->getChangedTime(), 'medium', '', NULL, $langcode);
          break;
      }
    }
    if ($author_tokens = $token_service
      ->findWithPrefix($tokens, 'author')) {
      $replacements += $token_service
        ->generate('user', $author_tokens, array(
        'user' => $log
          ->getOwner(),
      ), $options, $bubbleable_metadata);
    }
    if ($created_tokens = $token_service
      ->findWithPrefix($tokens, 'created')) {
      $replacements += $token_service
        ->generate('date', $created_tokens, array(
        'date' => $log
          ->getCreatedTime(),
      ), $options, $bubbleable_metadata);
    }
    if ($changed_tokens = $token_service
      ->findWithPrefix($tokens, 'changed')) {
      $replacements += $token_service
        ->generate('date', $changed_tokens, array(
        'date' => $log
          ->getChangedTime(),
      ), $options, $bubbleable_metadata);
    }
  }
  return $replacements;
}