You are here

function access_unpublished_tokens in Access unpublished 8

Implements hook_tokens().

File

./access_unpublished.tokens.inc, line 40
Contains access_unpublished token includes.

Code

function access_unpublished_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  if (empty($data['entity']) || !$data['entity'] instanceof EntityInterface || !AccessUnpublished::applicableEntityType($data['entity']
    ->getEntityType())) {
    return [];
  }
  $replacements = [];
  $entity = $data['entity'];

  /** @var \Drupal\access_unpublished\AccessTokenManager $accessTokenManager */
  $accessTokenManager = \Drupal::service('access_unpublished.access_token_manager');
  foreach ($tokens as $name => $original) {
    switch ($name) {
      case 'access-unpublished-url':
        $token = $accessTokenManager
          ->getActiveAccessToken($entity);
        $url = '';
        if ($token) {
          $url = $accessTokenManager
            ->getAccessTokenUrl($token, $entity
            ->language());
        }
        $replacements[$original] = $url;
        break;
    }
  }
  return $replacements;
}