You are here

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

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 98
A generic substitution for Drupal 8 Random utility.

Class

SchemaMetatagManager
Class SchemaMetatagManager.

Code

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_type = $entity->entity_type;
    $entity = menu_get_object($entity_type);
  }

  // Get all the metatags for this entity.
  $elements = metatag_generate_entity_metatags($entity, $entity_type);

  // 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_render(self::renderArrayJsonLd($jsonld));
    }
  }
}