You are here

public function YoastSeoController::tokens in Real-time SEO for Drupal 8

Returns a set of tokens' values.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request of the page.

  • data The context to use to retrieve the tokens value, see Drupal\Core\Utility\token::replace()
  • tokens An array of tokens to get the values for.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException In case of AccessDeniedException.

\Symfony\Component\HttpKernel\Exception\NotFoundHttpException In case of NotFoundHttpException.

1 string reference to 'YoastSeoController::tokens'
yoast_seo.routing.yml in ./yoast_seo.routing.yml
yoast_seo.routing.yml

File

src/Controller/YoastSeoController.php, line 32

Class

YoastSeoController
YoastSeoController.

Namespace

Drupal\yoast_seo\Controller

Code

public function tokens(Request $request) {

  /** @var MetatagToken $metatag_token */
  $metatag_token = \Drupal::service('metatag.token');
  $token_values = array();
  $tokens = $request->request
    ->get('tokens');
  $data = $request->request
    ->get('data');
  if (is_null($data)) {
    $data = array();
  }

  // Retrieve the tokens values.
  // Use the metatag token service, which use either core or token module
  // regarding if this one is installed.
  foreach ($tokens as $token) {
    $token_values[$token] = $metatag_token
      ->replace($token, $data);
  }
  return new JsonResponse($token_values);
}