You are here

function entity_legal_tokens in Entity Legal 3.0.x

Same name and namespace in other branches
  1. 8.2 entity_legal.tokens.inc \entity_legal_tokens()
  2. 4.0.x entity_legal.tokens.inc \entity_legal_tokens()

Implements hook_tokens().

File

./entity_legal.tokens.inc, line 109
Builds placeholder replacement tokens for entity_legal.

Code

function entity_legal_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::service('token');
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
  }
  $replacements = [];
  switch ($type) {
    case ENTITY_LEGAL_DOCUMENT_ENTITY_NAME:
      if (isset($data[ENTITY_LEGAL_DOCUMENT_ENTITY_NAME])) {

        /** @var \Drupal\entity_legal\EntityLegalDocumentInterface $entity */
        $entity = $data[ENTITY_LEGAL_DOCUMENT_ENTITY_NAME];
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'label':
              $replacements[$original] = $entity
                ->label();
              break;
            case 'url':
              $replacements[$original] = $entity
                ->toUrl('canonical', $url_options)
                ->toString();
              break;
            case 'published-version':
              $replacements[$original] = $entity
                ->getPublishedVersion()
                ->label();
              break;
          }
        }
        if ($published_version_tokens = $token_service
          ->findWithPrefix($tokens, 'published-version')) {
          $pubished_version = $entity
            ->getPublishedVersion();
          $replacements += $token_service
            ->generate(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, $published_version_tokens, [
            ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME => $pubished_version,
          ], $options, $bubbleable_metadata);
        }
      }
      break;
    case ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME:
      if (isset($data[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME])) {

        /** @var \Drupal\entity_legal\EntityLegalDocumentVersionInterface $entity */
        $entity = $data[ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME];
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'acceptance-label':
              $replacements[$original] = $entity
                ->get('acceptance_label')->value;
              break;
            case 'changed':
              $replacements[$original] = $entity
                ->getFormattedDate('changed');
              break;
            case 'created':
              $replacements[$original] = $entity
                ->getFormattedDate('created');
              break;
            case 'label':
              $replacements[$original] = $entity
                ->label();
              break;
          }
        }
        if ($changed_tokens = $token_service
          ->findWithPrefix($tokens, 'changed')) {
          $replacements += $token_service
            ->generate('date', $changed_tokens, [
            'date' => $entity
              ->getChangedTime(),
          ], $options, $bubbleable_metadata);
        }
        if ($created_tokens = $token_service
          ->findWithPrefix($tokens, 'created')) {
          $replacements += $token_service
            ->generate('date', $created_tokens, [
            'date' => $entity
              ->getCreatedTime(),
          ], $options, $bubbleable_metadata);
        }
      }
      break;
    case ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME:
      if (isset($data[ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME])) {

        /** @var \Drupal\entity_legal\EntityLegalDocumentAcceptanceInterface $entity */
        $entity = $data[ENTITY_LEGAL_DOCUMENT_ACCEPTANCE_ENTITY_NAME];
        foreach ($tokens as $name => $original) {
          switch ($name) {
            case 'acceptance-date':
              $replacements[$original] = \Drupal::service('date.formatter')
                ->format($entity
                ->get('acceptance_date')->value);
              break;
            case 'aid':
              $replacements[$original] = $entity
                ->id();
              break;
            case 'author':

              /** @var \Drupal\Core\Field\EntityReferenceFieldItemListInterface $item */
              $item = $entity
                ->get('uid');
              $replacements[$original] = $item
                ->referencedEntities()[0]
                ->label();
              break;
            case 'document-version':
              $replacements[$original] = $entity
                ->getDocumentVersion()
                ->label();
              break;
          }
        }
        if ($accepted_date_tokens = $token_service
          ->findWithPrefix($tokens, 'acceptance-date')) {
          $replacements += $token_service
            ->generate('date', $accepted_date_tokens, [
            'date' => $entity
              ->get('acceptance_date')->value,
          ], $options, $bubbleable_metadata);
        }
        if ($document_version_tokens = $token_service
          ->findWithPrefix($tokens, 'document-version')) {
          $document_version = $entity
            ->getDocumentVersion();
          $replacements += $token_service
            ->generate(ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME, $document_version_tokens, [
            ENTITY_LEGAL_DOCUMENT_VERSION_ENTITY_NAME => $document_version,
          ], $options, $bubbleable_metadata);
        }
      }
      break;
  }
  return $replacements;
}