Commit 36ca1990 authored by Damien McKenna's avatar Damien McKenna Committed by Damien McKenna
Browse files

Issue #3299222 by DamienMcKenna: Mobile tags are not overwriting core's tags as expected.

parent b3a1ff84
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ Metatag 8.x-1.x-dev, 2022-xx-xx
  for a given entity.
#3314204 by DamienMcKenna, joshua.boltz: Handling of single vs multiple-value
  metatags with Search API integration.
#3299222 by DamienMcKenna: Mobile tags are not overwriting core'\''s tags as
  expected.


Metatag 8.x-1.22, 2022-09-29
+17 −9
Original line number Diff line number Diff line
@@ -48,31 +48,39 @@ function metatag_mobile_theme() {
function metatag_mobile_page_attachments_alter(array &$attachments) {
  if (isset($attachments['#attached']['html_head'])) {
    // A list of core tags that will be replaced, in the format:
    // core tag => Metatag-supplied tag
    // core tag => Metatag-supplied tag(s)
    // This assumes that the module's meta tags are output *before* the core
    // tags, if this changes then We're Going To Have A Bad Time.
    // tags, if this changes then We're Going To Have A Bad Time. Also, the
    // Metatag output can have a "_0" suffix, which is why they need to be in
    // an array.
    $dupes = [
      'MobileOptimized' => 'mobileoptimized',
      'HandheldFriendly' => 'handheldfriendly',
      'viewport' => 'viewport',
      'MobileOptimized' => ['mobileoptimized', 'mobileoptimized_0'],
      'HandheldFriendly' => ['handheldfriendly', 'handheldfriendly_0'],
      'viewport' => ['viewport'],
    ];

    // Keep track of when the Metatag-supplied meta tags are found, so if the
    // core tag is also found it can be removed.
    $found = [];

    foreach ($dupes as $core_tag => $meta_tag) {
    foreach ($dupes as $core_tag => $meta_tags) {
      foreach ($attachments['#attached']['html_head'] as $key => $item) {
        if (isset($item[1])) {
          // The Metatag values are output before core's, so skip the first item
          // found so it can be picked up as the dupe; this is important for the
          // "viewport" meta tag where both core and Metatag use the same name.
          if ($item[1] == $meta_tag && !isset($found[$meta_tag])) {
            $found[$meta_tag] = $key;
          if (in_array($item[1], $meta_tags) && !isset($found[$core_tag])) {
            foreach ($meta_tags as $meta_tag) {
              if (!isset($found[$core_tag])) {
                $found[$core_tag] = $key;
              }
          elseif ($item[1] == $core_tag && isset($found[$meta_tag])) {
            }
          }
          elseif ($item[1] == $core_tag && isset($found[$core_tag])) {
            // @todo This ought to work, but doesn't?
            // @code
            // $attachments['#attached']['html_head'][$key]['#access'] = FALSE;
            // @endcode
            unset($attachments['#attached']['html_head'][$key]);
          }
        }
+109 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\metatag_mobile\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\metatag\Functional\MetatagHelperTrait;
use Drupal\user\Entity\User;

/**
 * Verify that the configured defaults load as intended.
 *
 * @group metatag
 */
class TestCoreTagRemoval extends BrowserTestBase {

  // Contains helper methods.
  use MetatagHelperTrait;

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    // These are needed for the tests.
    'node',
    'field_ui',

    // This module.
    'metatag_mobile',
  ];

  /**
   * Use the full install profile, with the full theme.
   */
  protected $profile = 'standard';

  /**
   * {@inheritdoc}
   */
  protected function setUp(): void {
    parent::setUp();

    // Log in as user 1.
    $this->loginUser1();

    // Add the Metatag field to the content type.
    $this->drupalGet('admin/structure/types/manage/page/fields/add-field');
    $this->assertSession()->statusCodeEquals(200);
    $edit = [
      'label' => 'Metatag',
      'field_name' => 'metatag',
      'new_storage_type' => 'metatag',
    ];
    $this->submitForm($edit, 'Save and continue');
    $this->submitForm([], 'Save field settings');
    $this->assertSession()->statusCodeEquals(200);
  }


  /**
   * Verify that core's duplicate meta tags are removed.
   */
  public function testCoreTagRemoval() {
    // Create a node that does not override core's meta tags.
    $this->drupalGet('node/add/page');
    $this->assertSession()->statusCodeEquals(200);
    $edit = [
      'title[0][value]' => 'Testing core tags',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Basic page Testing core tags has been created');

    // Verify each of the meta tags that should be removed by core..
    $xpath = $this->xpath("//meta[@name='HandheldFriendly']");
    $this->assertEquals(count($xpath), 1);
    $this->assertEquals((string) $xpath[0]->getAttribute('content'), 'true');
    $xpath = $this->xpath("//meta[@name='MobileOptimized']");
    $this->assertEquals(count($xpath), 1);
    $this->assertEquals((string) $xpath[0]->getAttribute('content'), 'width');
    $xpath = $this->xpath("//meta[@name='viewport']");
    $this->assertEquals(count($xpath), 1);
    $this->assertEquals((string) $xpath[0]->getAttribute('content'), 'width=device-width, initial-scale=1.0');

    // Create a second node that overrides core's meta tags.
    $this->drupalGet('node/add/page');
    $this->assertSession()->statusCodeEquals(200);
    $edit = [
      'title[0][value]' => 'Testing removal of core tags',
      'field_metatag[0][mobile][handheldfriendly]' => 'handheld friendly tag',
      'field_metatag[0][mobile][mobileoptimized]' => 'mobile optimized tag',
      'field_metatag[0][mobile][viewport]' => 'viewport tag',
    ];
    $this->submitForm($edit, 'Save');
    $this->assertSession()->statusCodeEquals(200);
    $this->assertSession()->pageTextContains('Basic page Testing removal of core tags has been created');

    // Verify that Metatag's tags are showing correctly.
    $xpath = $this->xpath("//meta[@name='HandheldFriendly']");
    $this->assertEquals(count($xpath), 1);
    $this->assertEquals((string) $xpath[0]->getAttribute('content'), 'handheld friendly tag');
    $xpath = $this->xpath("//meta[@name='MobileOptimized']");
    $this->assertEquals(count($xpath), 1);
    $this->assertEquals((string) $xpath[0]->getAttribute('content'), 'mobile optimized tag');
    $xpath = $this->xpath("//meta[@name='viewport']");
    $this->assertEquals(count($xpath), 1);
    $this->assertEquals((string) $xpath[0]->getAttribute('content'), 'viewport tag');
  }

}