Commit 642a912d authored by Damien McKenna's avatar Damien McKenna Committed by Damien McKenna
Browse files

Issue #3362522 by DamienMcKenna: Fix meta tag output sort order.

parent 9be55a9c
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,6 +7,7 @@ Metatag 8.x-1.x-dev, 2023-xx-xx
  tag.
#3106384 by DamienMcKenna: Remove the NOODP option on ROBOTS tag.
#3298064 by DamienMcKenna: Deprecate Google CSE Ratings tag.
#3362522 by DamienMcKenna: Fix meta tag output sort order.


Metatag 8.x-1.23, 2023-05-12
+1 −0
Original line number Diff line number Diff line
@@ -481,6 +481,7 @@ function metatag_preprocess_html(&$variables) {
 *   Array of meta tags or NULL.
 */
function metatag_get_tags_from_route($entity = NULL) {
  /** @var \Drupal\metatag\MetatagManager $metatag_manager */
  $metatag_manager = \Drupal::service('metatag.manager');

  // First, get defaults.
+1 −1
Original line number Diff line number Diff line
@@ -271,7 +271,7 @@ class MetatagDefaultsForm extends EntityForm {
    }

    // Set tags within the Metatag entity.
    $tags = $this->metatagPluginManager->getDefinitions();
    $tags = $this->metatagManager->sortedTags();
    $tag_values = [];
    foreach ($tags as $tag_id => $tag_definition) {
      if ($form_state->hasValue($tag_id)) {
+1 −3
Original line number Diff line number Diff line
@@ -110,7 +110,6 @@ class MetatagSettingsForm extends ConfigFormBase {

    $trimSettingsMaxlength = $this->config('metatag.settings')->get('tag_trim_maxlength');
    $trimMethod = $this->config('metatag.settings')->get('tag_trim_method');
    $metatags = $this->tagPluginManager->getDefinitions();

    $form['tag_trim'] = [
      '#title' => $this->t('Metatag Trimming Options'),
@@ -140,9 +139,8 @@ class MetatagSettingsForm extends ConfigFormBase {

    // Name the variable "metatag_id" to avoid confusing this with the "name"
    // value from the meta tag plugin as it's actually the plugin ID.
    foreach ($metatags as $metatag_id => $metatag_info) {
    foreach ($this->metatagManager->sortedTags() as $metatag_id => $metatag_info) {
      if (!empty($metatag_info['trimmable'])) {

        $form['tag_trim']['maxlength']['metatag_maxlength_' . $metatag_id] = [
          '#title' => $this->t('Meta Tags:') . ' ' . $metatag_id . ' ' . $this->t('length'),
          '#type' => 'number',
+32 −35
Original line number Diff line number Diff line
@@ -232,20 +232,16 @@ class MetatagManager implements MetatagManagerInterface {
    // Pull the data from the definitions into a new array.
    $groups = [];
    foreach ($metatag_groups as $group_name => $group_info) {
      $groups[$group_name]['id'] = $group_info['id'];
      $groups[$group_name] = $group_info;
      $groups[$group_name]['label'] = $group_info['label']->render();
      $groups[$group_name]['description'] = $group_info['description'] ?? '';
      $groups[$group_name]['weight'] = $group_info['weight'];
    }

    // Create the 'sort by' array.
    $sort_by = [];
    foreach ($groups as $group) {
      $sort_by[] = $group['weight'];
    }

    // Sort the groups by weight.
    array_multisort($sort_by, SORT_ASC, $groups);
    // Sort the tag groups.
    uasort($groups, [
      'Drupal\Component\Utility\SortArray',
      'sortByWeightElement',
    ]);

    return $groups;
  }
@@ -259,23 +255,28 @@ class MetatagManager implements MetatagManagerInterface {
    // Pull the data from the definitions into a new array.
    $tags = [];
    foreach ($metatag_tags as $tag_name => $tag_info) {
      $tags[$tag_name]['id'] = $tag_info['id'];
      $tags[$tag_name]['label'] = $tag_info['label']->render();
      $tags[$tag_name]['group'] = $tag_info['group'];
      $tags[$tag_name]['weight'] = $tag_info['weight'];
      $tags[$tag_info['group']][$tag_name] = $tag_info;
      $tags[$tag_info['group']][$tag_name]['label'] = $tag_info['label']->render();
    }

    // Create the 'sort by' array.
    $sort_by = [];
    foreach ($tags as $key => $tag) {
      $sort_by['group'][$key] = $tag['group'];
      $sort_by['weight'][$key] = $tag['weight'];
    }
    // Sort the tags based on the group.
    $sorted_tags = [];
    foreach ($this->sortedGroups() as $group_name => $group) {
      $tag_weight = $group['weight'] * 100;

    // Sort the tags by weight.
    array_multisort($sort_by['group'], SORT_ASC, $sort_by['weight'], SORT_ASC, $tags);
      // First, sort the tags within the group according to the original sort
      // order provided by the tag's definition.
      uasort($tags[$group_name], [
        'Drupal\Component\Utility\SortArray',
        'sortByWeightElement',
      ]);
      foreach ($tags[$group_name] as $tag_name => $tag_info) {
        $tag_info['weight'] = $tag_weight++;
        $sorted_tags[$tag_name] = $tag_info;
      }
    }

    return $tags;
    return $sorted_tags;
  }

  /**
@@ -573,7 +574,6 @@ class MetatagManager implements MetatagManagerInterface {
    // 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.
@@ -584,8 +584,6 @@ class MetatagManager implements MetatagManagerInterface {
        $token_replacements = [$entity->getEntityTypeId() => $entity];
      }
    }
    $rawTags = [];
    $metatag_tags = $this->tagPluginManager->getDefinitions();

    // Use the entity's language code, if one is defined.
    $langcode = NULL;
@@ -593,13 +591,11 @@ class MetatagManager implements MetatagManagerInterface {
      $langcode = $entity->language()->getId();
    }

    // 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);
    $definitions = $this->sortedTags();

    // Sort the meta tags so they are rendered in the correct order.
    $ordered_tags = [];
    foreach ($metatag_tags as $id => $metatag) {
    foreach ($definitions as $id => $metatag) {
      if (isset($tags[$id])) {
        $ordered_tags[$id] = $tags[$id];
      }
@@ -607,9 +603,10 @@ class MetatagManager implements MetatagManagerInterface {

    // Each element of the $values array is a tag with the tag plugin name as
    // the key.
    $rawTags = [];
    foreach ($ordered_tags as $tag_name => $value) {
      // Check to ensure there is a matching plugin.
      if (isset($metatag_tags[$tag_name])) {
      if (isset($definitions[$tag_name])) {
        // Get an instance of the plugin.
        $tag = $this->tagPluginManager->createInstance($tag_name);

@@ -673,7 +670,7 @@ class MetatagManager implements MetatagManagerInterface {
    }

    if (!isset($this->processedTokenCache[$entity_identifier])) {
      $metatag_tags = $this->tagPluginManager->getDefinitions();
      $metatag_tags = $this->sortedTags();

      // Each element of the $values array is a tag with the tag plugin name as
      // the key.
Loading