You are here

public function ApiTokenBase::process in API Tokens 8.2

Same name and namespace in other branches
  1. 8 src/ApiTokenBase.php \Drupal\api_tokens\ApiTokenBase::process()

Returns processed API token build.

Return value

array A renderable array for the API token output.

Overrides ApiTokenPluginInterface::process

File

src/ApiTokenBase.php, line 270

Class

ApiTokenBase
Provides a base class for the API token plugins.

Namespace

Drupal\api_tokens

Code

public function process() {
  static $recursion = FALSE;
  if ($recursion) {
    return [];
  }
  $key = "{$this->pluginId}:{$this->hash}";
  if (in_array($key, self::$context)) {
    $recursion = TRUE;
    $this->logger
      ->warning($this
      ->t('Recursion detected while rendering @token API token.', [
      '@token' => $this->token,
    ]));
    return [];
  }
  array_push(self::$context, $key);
  $build = call_user_func_array([
    $this,
    'build',
  ], $this->params);
  $this->moduleHandler
    ->alter('api_token_build', $build, $this);
  $this->renderer
    ->renderPlain($build);
  array_pop(self::$context);
  if ($recursion) {
    self::$context || ($recursion = FALSE);
    return [];
  }
  $this
    ->addCacheableDependency(CacheableMetadata::createFromRenderArray($build));
  $build = [
    '#markup' => $build['#markup'],
    '#attached' => $build['#attached'],
    '#cache' => [
      'contexts' => $this
        ->getCacheContexts(),
      'tags' => $this
        ->getCacheTags(),
      'max-age' => $this
        ->getCacheMaxAge(),
    ],
  ];
  return $build;
}