public static function SchemaMetatagManager::parseJsonld in Schema.org Metatag 8
Same name and namespace in other branches
- 8.2 src/SchemaMetatagManager.php \Drupal\schema_metatag\SchemaMetatagManager::parseJsonld()
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
1 call to SchemaMetatagManager::parseJsonld()
- SchemaMetatagManager::getRenderedJsonld in src/
SchemaMetatagManager.php - Render JSON LD for a specific entity.
File
- src/
SchemaMetatagManager.php, line 17
Class
- SchemaMetatagManager
- Class SchemaMetatagManager.
Namespace
Drupal\schema_metatagCode
public static function parseJsonld(array &$elements) {
// Elements are in indeterminable order.
// First time through, collect and nest by group.
$schema_metatags = [];
foreach ($elements as $key => $item) {
if (!empty($item[0]['#attributes']['schema_metatag'])) {
$group = $item[0]['#attributes']['group'];
// Nest items by the group they are in.
$name = $item[0]['#attributes']['name'];
$content = $item[0]['#attributes']['content'];
$schema_metatags[$group][$name] = $content;
unset($elements[$key]);
}
}
// Second time through, replace group name with index,
// and add JSON LD wrappers.
$items = [];
$group_key = 0;
foreach ($schema_metatags as $data) {
if (empty($items)) {
$items['@context'] = 'https://schema.org';
}
if (!empty($data)) {
$items['@graph'][$group_key] = $data;
}
$group_key++;
}
return $items;
}