Commit 5de8438b authored by Damien McKenna's avatar Damien McKenna Committed by Damien McKenna
Browse files

Issue #3326103 by DamienMcKenna, harshitthakore: Remove the old TypedData...

Issue #3326103 by DamienMcKenna, harshitthakore: Remove the old TypedData plugin for Search API integration.
parent 89b221db
Loading
Loading
Loading
Loading
+2 −0
Changes for CHANGELOG.txt: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -3,6 +3,8 @@ Metatag 2.0.x-dev, 2022-xx-xx
#3164404 by DamienMcKenna: Rewrite tags testing architecture.
#3305586 by DamienMcKenna: Remove Panelizer support.
#3272768 by DamienMcKenna, SandeepSingh199: Convert to JSON data storage.
#3326103 by DamienMcKenna, harshitthakore: Remove the old TypedData plugin for
  Search API integration.


Metatag 8.x-1.x-dev, 2022-xx-xx
+0 −43
Changes for README.md: 0 added lines, 43 removed lines.
Original line number Diff line number Diff line
@@ -135,49 +135,6 @@ Metatag field entirely, then use tokens for those fields in the defaults
display, or just left hidden.


## Indexing meta tags with Search API

The Search API integration allows indexing the meta tag values from a meta tag
field on an entity bundle.

### Limitations

* Each meta tag must be specified individually, there isn't (currently) a way to
  add all of them at once.
* This may not work with values stored in Schema.org data structures from the
  Schema.org Metatag module.
* Changing the global Metatag defaults will not trigger a reindex of the content
  so this will need to be done separately.
* By default meta tags will be added as "string" fields, which will not include
  them in the keyword search index; their "Type" selection must be specifically
  set to "Fulltext" in order for the meta tags to be included in the keyword
  search index, otherwise they will only be useful for doing facets or filters.

### Setup

To add meta tags to the Search API:

* Make sure that a Metatag field is present on the entity bundle(s) that are
  being indexed.
* In the index configuration, modify the Fields section.
* Click the "Add fields" button.
* In the "Add fields to index [indexname]" popup, scroll down to find the
  Metatag field; it will have the name defined in the field's settings.
* Click the plus `(+)` button beside the field to see which meta tags are
  available.
* Click the "Add" button for the meta tags that are needed.
* Click "Done" when all of the meta tags are added.
* Look for the meta tag fields added to the index, change their "type" selector
  to "Fulltext"; adjust the "Boost" value if desired.
* Click "Save changes" at the bottom of the Fields admin page.
* Reindex the content with the new settings.

### Deprecated

Note that this specific Search API integration has been deprecated in release
8.x-1.23 and a replacement will be added separately to a future release.


## Alternative option to simplify the content administration experience

On the settings page (/admin/config/search/metatag/settings) are options to
+0 −14
Changes for src/Plugin/Field/FieldType/MetatagFieldItem.php: 0 added lines, 14 removed lines.
Original line number Diff line number Diff line
@@ -47,20 +47,6 @@ class MetatagFieldItem extends FieldItemBase {
      ->setLabel(t('Metatag'))
      ->setRequired(TRUE);

    $sorted_tags = \Drupal::service('metatag.manager')->sortedGroupsWithTags();

    foreach ($sorted_tags as $group_id => $group) {
      if (isset($group['tags'])) {
        foreach ($group['tags'] as $tag_id => $tag) {
          $properties[$tag_id] = DataDefinition::create('string')
            ->setLabel(t('@label', ['@label' => $tag['label']]))
            ->setComputed(TRUE)
            ->setClass('\Drupal\metatag\TypedData\IndividualTag')
            ->setSetting('tag_name', $tag_id);
        }
      }
    }

    return $properties;
  }

src/TypedData/IndividualTag.php

deleted100644 → 0
+0 −105
Changes for src/TypedData/IndividualTag.php: 0 added lines, 105 removed lines.
Original line number Diff line number Diff line
<?php

namespace Drupal\metatag\TypedData;

use Drupal\Core\TypedData\DataDefinitionInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\TypedData\TypedData;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;

/**
 * A computed property for each meta tag.
 *
 * Required settings (below the definition's 'settings' key) are:
 *  - tag_name: The tag to be processed.
 *    Examples: "title", "description".
 *
 * @deprecated in metatag:8.x-1.23 and is removed from metatag:2.0.0. A replacement will be provided separately.
 *
 * @see https://www.drupal.org/node/3326104
 */
class IndividualTag extends TypedData {

  use DependencySerializationTrait;

  /**
   * Cached processed value.
   */
  protected $value;

  /**
   * {@inheritdoc}
   */
  public function __construct(DataDefinitionInterface $definition, $name = NULL, TypedDataInterface $parent = NULL) {
    parent::__construct($definition, $name, $parent);

    if ($definition->getSetting('tag_name') === NULL) {
      throw new \InvalidArgumentException("The definition's 'tag_name' key has to specify the name of the meta tag to be processed.");
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    if (isset($this->value)) {
      return $this->value;
    }

    // The Metatag plugin ID.
    $property_name = $this->definition->getSetting('tag_name');

    // The item is the parent.
    $item = $this->getParent();
    $entity = $item->getEntity();

    // Rendered values.
    $metatagManager = \Drupal::service('metatag.manager');
    $defaultTags = $metatagManager->tagsFromEntityWithDefaults($entity);
    if (!isset($defaultTags[$property_name])) {
      \Drupal::service('logger.factory')
        ->get('metatag')
        ->notice('No default for "%tag_name" - entity_type: %type, entity_bundle: %bundle, id: %id. See src/TypedData/Metatags.php.', [
          '%tag_name' => $property_name,
          '%type' => $entity->getEntityTypeId(),
          '%bundle' => $entity->bundle(),
          '%id' => $entity->id(),
        ]);
      return FALSE;
    }
    $tags = [
      $property_name => $defaultTags[$property_name],
    ];
    $values = $metatagManager->generateRawElements($tags, $entity);

    $all_tags = [];
    foreach (\Drupal::service('plugin.manager.metatag.tag')->getDefinitions() as $tag_name => $tag_spec) {
      $all_tags[$tag_name] = new $tag_spec['class']([], $tag_name, $tag_spec);
    }

    // If this tag has a value set the property value.
    if (isset($values[$property_name])) {
      $attribute_name = $all_tags[$property_name]->getHtmlValueAttribute();

      // It should be possible to extract the HTML attribute that stores the
      // value, but in some cases it might not be possible.
      if (isset($values[$property_name]['#attributes'][$attribute_name])) {
        $this->value = $values[$property_name]['#attributes'][$attribute_name];
      }
      else {
        \Drupal::service('logger.factory')
          ->get('metatag')
          ->notice('Attribute value not mapped for "%property_name" - entity_type: %type, entity_bundle: %bundle, id: %id. See src/TypedData/Metatags.php.', [
            '%property_name' => $property_name,
            '%type' => $entity->getEntityTypeId(),
            '%bundle' => $entity->bundle(),
            '%id' => $entity->id(),
          ]);
        return FALSE;
      }
    }

    return $this->value;
  }

}