public static function SchemaMetatagManager::getRenderedJsonld in Schema.org Metatag 8
Same name and namespace in other branches
- 8.2 src/SchemaMetatagManager.php \Drupal\schema_metatag\SchemaMetatagManager::getRenderedJsonld()
Render JSON LD for a specific entity.
Useful to pass to a decoupled front end, for instance.
Parameters
string $entity: The entity that contains JSONLD.
string $entity_type: The type of entity.
Return value
string The JSONLD markup.
Overrides SchemaMetatagManagerInterface::getRenderedJsonld
File
- src/
SchemaMetatagManager.php, line 76
Class
- SchemaMetatagManager
- Class SchemaMetatagManager.
Namespace
Drupal\schema_metatagCode
public static function getRenderedJsonld($entity = NULL, $entity_type = NULL) {
// If nothing was passed in, assume the current entity.
// @see schema_metatag_entity_load() to understand why this works.
if (empty($entity)) {
$entity = metatag_get_route_entity();
}
// Get all the metatags for this entity.
$metatag_manager = \Drupal::service('metatag.manager');
if (!empty($entity) && $entity instanceof ContentEntityInterface) {
foreach ($metatag_manager
->tagsFromEntity($entity) as $tag => $data) {
$metatags[$tag] = $data;
}
}
// Trigger hook_metatags_alter().
// Allow modules to override tags or the entity used for token replacements.
$context = [
'entity' => $entity,
];
\Drupal::service('module_handler')
->alter('metatags', $metatags, $context);
$elements = $metatag_manager
->generateElements($metatags, $entity);
// Parse the Schema.org metatags out of the array.
if ($items = self::parseJsonld($elements)) {
// Encode the Schema.org metatags as JSON LD.
if ($jsonld = self::encodeJsonld($items)) {
// Pass back the rendered result.
return \Drupal::service('renderer')
->render(self::renderArrayJsonLd($jsonld));
}
}
}