You are here

public static function SchemaMetatagManager::parseJsonld in Schema.org Metatag 7

Parse tags added by Schema Metatag into JsonLD array.

Parameters

array $elements: Array of Metatag values, as formatted for the head of a page.

Return value

array Array of Schema metatag tags, ready to be turned into JSON LD.

Overrides SchemaMetatagManagerInterface::parseJsonld

2 calls to SchemaMetatagManager::parseJsonld()
SchemaMetatagManager::getRenderedJsonld in src/SchemaMetatagManager.php
Render JSON LD for a specific entity.
schema_metatag_html_head_alter in ./schema_metatag.module
Implements hook_html_head_alter().

File

src/SchemaMetatagManager.php, line 44
A generic substitution for Drupal 8 Random utility.

Class

SchemaMetatagManager
Class SchemaMetatagManager.

Code

public static function parseJsonld(&$elements) {
  $schema_metatags = [];
  foreach ($elements as $key => $info) {
    if (!empty($info['#attributes']['schema_metatag'])) {

      // Nest tags by group.
      $group = $info['#attributes']['group'];
      $name = $info['#attributes']['name'];
      $value = $info['#attributes']['content'];
      $schema_metatags[$group][$name] = $value;

      // Remove this tag from the elements array.
      unset($elements[$key]);
    }
  }
  $items = [];
  foreach ($schema_metatags as $data) {
    if (empty($items)) {
      $items['@context'] = 'https://schema.org';
    }
    if (!empty($data)) {
      $items['@graph'][] = $data;
    }
  }
  return $items;
}