Commit 9459e192 authored by Damien McKenna's avatar Damien McKenna Committed by Damien McKenna
Browse files

Issue #3115634 by DamienMcKenna: Refactor...

Issue #3115634 by DamienMcKenna: Refactor MetatagManager::generateRawElements() to create token arguments once per call
parent 93c69105
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ By DamienMcKenna: Minor code readability tweak.
  through child classes $tags property; updated all tests as necessary.
#3114120 by felribeiro, DamienMcKenna: Recommend "description" tag be less than
  ~160 characters.
#3115634 by DamienMcKenna: Refactor MetatagManager::generateRawElements() to
  create token arguments once per call.


Metatag 8.x-1.11, 2019-12-20
+20 −15
Original line number Diff line number Diff line
@@ -504,6 +504,25 @@ class MetatagManager implements MetatagManagerInterface {
      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();
@@ -524,27 +543,13 @@ class MetatagManager implements MetatagManagerInterface {
        // 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();

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

        // Now store the value with processed tokens back into the plugin.