Commit 7ed42d2d authored by Damien McKenna's avatar Damien McKenna
Browse files

Issue #3083743 by Eugene Bocharov, DamienMcKenna, bserem, gkannan25, osopolar,...

Issue #3083743 by Eugene Bocharov, DamienMcKenna, bserem, gkannan25, osopolar, timohuisman, akalam, zcht, druhu, maticb, vuil: Add Google's new robots tags.
parent d7a36a49
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
Metatag 8.x-1.x-dev, 2023-xx-xx
-------------------------------
#3083743 by Eugene Bocharov, DamienMcKenna, bserem, gkannan25, osopolar,
  timohuisman, akalam, zcht, druhu, maticb, vuil: Add Google's new robots tags.


Metatag 8.x-1.23, 2023-05-12
+4 −0
Original line number Diff line number Diff line
@@ -102,7 +102,11 @@ class MetatagDisplayExtender extends DisplayExtenderPluginBase {
      $metatags = $form_state->cleanValues()->getValues();
      $this->options['tokenize'] = $metatags['tokenize'] ?? FALSE;
      unset($metatags['tokenize']);
      $available_tags = array_keys($this->metatagTagManager->getDefinitions());
      foreach ($metatags as $tag_id => $tag_value) {
        if (!in_array($tag_id, $available_tags)) {
          continue;
        }
        // Some plugins need to process form input before storing it.
        // Hence, we set it and then get it.
        $tag = $this->metatagTagManager->createInstance($tag_id);
+70 −8
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\metatag\Plugin\metatag\Tag;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;

/**
@@ -44,13 +45,8 @@ class Robots extends MetaNameBase {
   * {@inheritdoc}
   */
  public function form(array $element = []) {
    // Prepare the default value as it is stored as a string.
    $default_value = [];
    if (!empty($this->value)) {
      $default_value = explode(', ', $this->value);
    }

    $form = [
    $form = [];
    $form['robots'] = [
      '#type' => 'checkboxes',
      '#title' => $this->label(),
      '#description' => $this->description(),
@@ -91,11 +87,59 @@ class Robots extends MetaNameBase {
          ],
        ],
      ],
      '#default_value' => $default_value,
      '#required' => $element['#required'] ?? FALSE,
      '#element_validate' => [[get_class($this), 'validateTag']],
    ];

    $form['robots-keyed'] = [
      '#type' => 'container',
      '#tree' => TRUE,
    ];

    $form['robots-keyed']['max-snippet'] = [
      '#type' => 'number',
      '#min' => -1,
      '#title' => $this->t('Max Snippet'),
      '#description' => $this->t('Use a number character as a textual snippet for this search result. "0" equals "nosnippet". "-1" will let the search engine decide the most effective length.'),
    ];

    $form['robots-keyed']['max-video-preview'] = [
      '#type' => 'number',
      '#min' => -1,
      '#title' => $this->t('Max Video Preview'),
      '#description' => $this->t('Use a maximum of number seconds as a video snippet for videos on this page in search results. "0" will use a static a image. "-1" means there is no limit.'),
    ];

    $form['robots-keyed']['max-image-preview'] = [
      '#type' => 'select',
      '#title' => $this->t('Max Image Preview'),
      '#description' => $this->t('Set the maximum size of an image preview for this page in a search results.'),
      '#options' => [
        'none' => $this->t('None - no image preview is to be shown.'),
        'standard' => $this->t('Standard - a default image preview may be shown.'),
        'large' => $this->t('Large - a larger image preview, up to the width of the viewport, may be shown.'),
      ],
      '#empty_option' => $this->t('Select'),
    ];

    $form['robots-keyed']['unavailable_after'] = [
      '#type' => 'date',
      '#title' => $this->t('Unavailable after date'),
      '#description' => $this->t('Do not show this page in search results after the specified date'),
    ];

    // Prepare the default value as it is stored as a string.
    if (!empty($this->value)) {
      $default_value = explode(', ', $this->value);
      $form['robots']['#default_value'] = $default_value;
      foreach ($default_value as $value) {
        $key_value = explode(':', $value);
        if (!empty($key_value[1]) && isset($form['robots-keyed'][$key_value[0]])) {
          $form['robots-keyed'][$key_value[0]]['#default_value'] = $key_value[1];
        }
      }
    }

    return $form;
  }

@@ -120,4 +164,22 @@ class Robots extends MetaNameBase {
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function validateTag(array &$element, FormStateInterface $form_state) {
    $robots_combined_value = $form_state->getValue($element['#parents']);
    $robots_root_parents = array_slice($element['#parents'], 0, -1);
    $robots_keyed = $form_state->getValue(array_merge($robots_root_parents, ['robots-keyed']));
    if (is_array($robots_keyed)) {
      foreach ($robots_keyed as $key => $value) {
        if (!empty($value)) {
          $option = "$key:$value";
          $robots_combined_value[$option] = $option;
        }
      }
      $form_state->setValue($robots_root_parents ?: $element['#parents'], $robots_combined_value);
    }
  }

}
+10 −4
Original line number Diff line number Diff line
@@ -191,17 +191,23 @@ class MetatagTagsTest extends MetatagTagsTestBase {
  }

  /**
   * Implements {tag_name}TestValue() for 'robots'.
   */
  protected function robotsTestKey() {
    return 'robots[index]';
   * Implements {tag_name}TestFormValues() for 'robots'.
   */
  protected function robotsTestFormValues() {
    return [
      'robots[index]' => 'index',
      'robots-keyed[max-snippet]' => 10,
      'robots-keyed[max-video-preview]' => 20,
      'robots-keyed[max-image-preview]' => 'none',
      'robots-keyed[unavailable_after]' => '2022-12-31',
    ];
  }

  /**
   * Implements {tag_name}TestValue() for 'robots'.
   */
  protected function robotsTestValue() {
    return 'index';
    return 'index, max-snippet:10, max-video-preview:20, max-image-preview:none, unavailable_after:2022-12-31';
  }

  /**
+21 −12
Original line number Diff line number Diff line
@@ -156,17 +156,6 @@ abstract class MetatagTagsTestBase extends BrowserTestBase {

      // Update the Global defaults and test them.
      $all_values = $values = [];
      // Look for a custom method named "{$tagname}TestKey", if found use
      // that method to get the test string for this meta tag, otherwise it
      // defaults to the meta tag's name.
      $method = $this->getMethodFromTagCallback($tag_name, 'TestKey');
      if (method_exists($this, $method)) {
        $test_key = $this->$method();
      }
      else {
        $test_key = $tag_name;
      }

      // Look for a custom method named "{$tagname}TestValue", if found use
      // that method to get the test string for this meta tag, otherwise it
      // defaults to just generating a random string.
@@ -192,8 +181,28 @@ abstract class MetatagTagsTestBase extends BrowserTestBase {
        $test_output = $test_value;
      }

      $values[$test_key] = $test_value;
      $all_values[$tag_name] = $test_output;
      // Look for a custom method named "{$tagname}TestFormValues", if found
      // use that method to get the keys/values array for the edit form. This
      // allows to test tags, that use more than one form element.
      $method = $this->getMethodFromTagCallback($tag_name, 'TestFormValues');
      if (method_exists($this, $method)) {
        $values = $this->$method();
      }
      else {
        // Look for a custom method named "{$tagname}TestKey", if found use
        // that method to get the test string for this meta tag, otherwise it
        // defaults to the meta tag's name.
        $method = $this->getMethodFromTagCallback($tag_name, 'TestKey');
        if (method_exists($this, $method)) {
          $test_key = $this->$method();
        }
        else {
          $test_key = $tag_name;
        }

        $values[$test_key] = $test_output;
      }
      $this->submitForm($values, 'Save');
      // Note: if this line fails then check that the failing meta tag has a
      // definition in the relevant *.metatag_tag.schema.yml file.