You are here

public function MetatagManager::generateRawElements in Metatag 8

Generate the actual meta tag values.

Parameters

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

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

\Drupal\Core\Render\BubbleableMetadata|null $cache: (optional) Cacheability metadata.

Return value

array Render array with tag elements.

1 call to MetatagManager::generateRawElements()
MetatagManager::generateElements in src/MetatagManager.php
Generate the elements that go in the hook_page_attachments attached array.

File

src/MetatagManager.php, line 520

Class

MetatagManager
Class MetatagManager.

Namespace

Drupal\metatag

Code

public function generateRawElements(array $tags, $entity = NULL, BubbleableMetadata $cache = NULL) {

  // Ignore the update.php path.
  $request = \Drupal::request();
  if ($request
    ->getBaseUrl() == '/update.php') {
    return [];
  }

  // Prepare any tokens that might exist.
  $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,
      ];
    }
  }

  // Ge the current language code.
  $langcode = \Drupal::languageManager()
    ->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)
    ->getId();
  $rawTags = [];
  $metatag_tags = $this->tagPluginManager
    ->getDefinitions();

  // Order metatags based on the group and weight.
  $group = array_column($metatag_tags, 'group');
  $weight = array_column($metatag_tags, 'weight');
  array_multisort($group, SORT_ASC, $weight, SORT_ASC, $metatag_tags);
  $ordered_tags = [];
  foreach ($metatag_tags as $id => $metatag) {
    if (isset($tags[$id])) {
      $ordered_tags[$id] = $tags[$id];
    }
  }

  // Each element of the $values array is a tag with the tag plugin name as
  // the key.
  foreach ($ordered_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);

      // 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);

      // Obtain the processed value.
      $processed_value = htmlspecialchars_decode($this->tokenService
        ->replace($tag
        ->value(), $token_replacements, [
        'langcode' => $langcode,
      ], $cache));

      // Now store the value with processed tokens back into the plugin.
      $tag
        ->setValue($processed_value);

      // Have the tag generate the output based on the value we gave it.
      $output = $tag
        ->output();
      if (!empty($output)) {
        $output = $tag
          ->multiple() ? $output : [
          $output,
        ];

        // Backwards compatibility for modules which don't support this logic.
        if (isset($output['#tag'])) {
          $output = [
            $output,
          ];
        }
        foreach ($output as $index => $element) {

          // Add index to tag name as suffix to avoid having same key.
          $index_tag_name = $tag
            ->multiple() ? $tag_name . '_' . $index : $tag_name;
          $rawTags[$index_tag_name] = $element;
        }
      }
    }
  }
  return $rawTags;
}