You are here

public function MetatagManager::generateTokenValues in Metatag 8

Generate the actual meta tag values for use as tokens.

Parameters

array $tags: The array of tags as plugin_id => value.

object $entity: Optional entity object to use for token replacements.

Return value

array Array of MetatagTag plugin instances.

File

src/MetatagManager.php, line 616

Class

MetatagManager
Class MetatagManager.

Namespace

Drupal\metatag

Code

public function generateTokenValues(array $tags, $entity = NULL) {

  // Ignore the update.php path.
  $request = \Drupal::request();
  if ($request
    ->getBaseUrl() == '/update.php') {
    return [];
  }
  $entity_identifier = '_none';
  if ($entity) {
    $entity_identifier = $entity
      ->getEntityTypeId() . ':' . ($entity
      ->uuid() ?: $entity
      ->id());
  }
  if (!isset($this->processedTokenCache[$entity_identifier])) {
    $metatag_tags = $this->tagPluginManager
      ->getDefinitions();

    // Each element of the $values array is a tag with the tag plugin name as
    // the key.
    foreach ($tags as $tag_name => $value) {

      // Check to ensure there is a matching plugin.
      if (isset($metatag_tags[$tag_name])) {

        // Get an instance of the plugin.
        $tag = $this->tagPluginManager
          ->createInstance($tag_name);

        // Render any tokens in the value.
        $token_replacements = [];
        if ($entity) {

          // @todo This needs a better way of discovering the context.
          if ($entity instanceof ViewEntityInterface) {

            // Views tokens require the ViewExecutable, not the config entity.
            // @todo Can we move this into metatag_views somehow?
            $token_replacements = [
              'view' => $entity
                ->getExecutable(),
            ];
          }
          elseif ($entity instanceof ContentEntityInterface) {
            $token_replacements = [
              $entity
                ->getEntityTypeId() => $entity,
            ];
          }
        }

        // Set the value as sometimes the data needs massaging, such as when
        // field defaults are used for the Robots field, which come as an
        // array that needs to be filtered and converted to a string.
        // @see Robots::setValue()
        $tag
          ->setValue($value);
        $langcode = \Drupal::languageManager()
          ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
          ->getId();
        $value = PlainTextOutput::renderFromHtml(htmlspecialchars_decode($this->tokenService
          ->replace($value, $token_replacements, [
          'langcode' => $langcode,
        ])));
        $this->processedTokenCache[$entity_identifier][$tag_name] = $tag
          ->multiple() ? explode(',', $value) : $value;
      }
    }
  }
  return $this->processedTokenCache[$entity_identifier];
}