Commit 9a405d6e authored by Damien McKenna's avatar Damien McKenna Committed by Damien McKenna
Browse files

Issue #3332348 by DamienMcKenna: Add function for getting list of overridden...

Issue #3332348 by DamienMcKenna: Add function for getting list of overridden meta tags for a given entity.
parent e50182d1
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -14,6 +14,8 @@ Metatag 8.x-1.x-dev, 2022-xx-xx
#3329362 by Project Update Bot, DamienMcKenna: Minor improvement to
  metatag.api.php.
#3323961 by ressa, DamienMcKenna: Update README.md with new standard format.
#3332348 by DamienMcKenna: Add function for getting list of overridden meta tags
  for a given entity.


Metatag 8.x-1.22, 2022-09-29
+8 −1
Original line number Diff line number Diff line
@@ -259,7 +259,7 @@ necessary to copy each value to the new record.
For developers needing to access the rendered meta tags for a given entity, a
function is provided to make this easy to do:

    $metatags = metatag_generate_entity_metatags($entity);
    $metatags = metatag_generate_entity_all_tags($entity);

This will return an array with the following structure:

@@ -301,6 +301,13 @@ the type "html_tag". Extracting the value of the meta tag will depend upon the
type of meta tag, e.g. the generator meta tag uses the "content" attribute while
the link tag uses the "href" attribute.

Another function exists for viewing only the meta tags that are overridden for
an entity, i.e. global defaults are not included.

    $metatags = metatag_generate_entity_overrides($entity);

This returns output in the same format as the function above.


## Migration / upgrade from Drupal 6 or 7

+43 −1
Original line number Diff line number Diff line
@@ -619,13 +619,30 @@ function metatag_entity_base_field_info(EntityTypeInterface $entity_type) {
/**
 * Turn the meta tags for an entity into a human readable structure.
 *
 * @param object $entity
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity object.
 *
 * @return array
 *   All of the meta tags in a nested structure.
 *
 * @deprecated in metatag:8.x-1.23 and is removed from metatag:2.0.0. Use metatag_generate_entity_all_tags() instead.
 *
 * @see https://www.drupal.org/node/
 */
function metatag_generate_entity_metatags($entity) {
  return metatag_generate_entity_all_tags($entity);
}

/**
 * Turn the meta tags for an entity into a human readable structure.
 *
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity object.
 *
 * @return array
 *   All of the meta tags in a nested structure.
 */
function metatag_generate_entity_all_tags(ContentEntityInterface $entity): array {
  $values = [];
  $raw = metatag_get_tags_from_route($entity);
  if (!empty($raw['#attached']['html_head'])) {
@@ -636,6 +653,31 @@ function metatag_generate_entity_metatags($entity) {
  return $values;
}

/**
 * Turn the meta tags for an entity into a human readable structure.
 *
 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
 *   The entity object.
 *
 * @return array
 *   The meta tags overridden on this entity in a nested structure.
 */
function metatag_generate_entity_overrides(ContentEntityInterface $entity): array {
  $values = [];
  $metatag_manager = \Drupal::service('metatag.manager');
  $metatags = [];
  foreach ($metatag_manager->tagsFromEntity($entity) as $tag => $data) {
    $metatags[$tag] = $data;
  }
  $raw = $metatag_manager->generateElements($metatags, $entity);
  if (!empty($raw['#attached']['html_head'])) {
    foreach ($raw['#attached']['html_head'] as $tag) {
      $values[$tag[1]] = $tag[0];
    }
  }
  return $values;
}

/**
 * Implements hook_migrate_prepare_row().
 */
+5 −0
Original line number Diff line number Diff line
@@ -429,6 +429,11 @@ abstract class MetatagFieldTestBase extends BrowserTestBase {
    }

    // @todo Confirm the values output correctly.

    // Check the output.
    // @todo Test this.
    $all_tags = metatag_generate_entity_all_tags($entity);
    $overrides = metatag_generate_entity_overrides($entity);
  }

  /**