You are here

function _entity_embed_render_placeholders in Entity Embed 7.3

Same name and namespace in other branches
  1. 7 entity_embed.module \_entity_embed_render_placeholders()
  2. 7.2 entity_embed.module \_entity_embed_render_placeholders()

Implements callback_filter_process().

Converts embedded entity placeholders into rendered entities.

1 string reference to '_entity_embed_render_placeholders'
entity_embed_filter_info in ./entity_embed.module
Implements hook_filter_info().

File

./entity_embed.module, line 484
Provides a CKEditor plugin and text filter for embedding and rendering entities.

Code

function _entity_embed_render_placeholders($text, $filter, $format, $langcode, $cache, $cache_id) {
  $result = $text;
  if (strpos($text, 'data-entity-type') !== FALSE && (strpos($text, 'data-entity-embed-display') !== FALSE || strpos($text, 'data-view-mode') !== FALSE)) {
    $dom = entity_embed_dom_load_html($text);
    $xpath = new \DOMXPath($dom);
    foreach ($xpath
      ->query('//drupal-entity[@data-entity-type and (@data-entity-uuid or @data-entity-id) and (@data-entity-embed-display or @data-view-mode)]') as $node) {
      $entity_type = $node
        ->getAttribute('data-entity-type');
      $entity = NULL;
      $entity_output = '';
      try {

        // Load the entity either by UUID (preferred) or ID.
        $uuid = $node
          ->getAttribute('data-entity-uuid');
        $id = $node
          ->getAttribute('data-entity-id');
        if (!empty($uuid) && module_exists('uuid')) {
          $entity = entity_uuid_load($entity_type, array(
            $uuid,
          ));
          $entity = reset($entity);
        }
        else {
          $entity = entity_load_single($entity_type, $id);
        }
        if ($entity) {

          // Protect ourselves from recursive rendering.
          static $depth = 0;
          $depth++;
          if ($depth > 20) {
            throw new EntityEmbedRecursiveRenderingException(sprintf('Recursive rendering detected when rendering embedded %s entity %s.', $entity_type, $id));
          }

          // If a UUID was not used, but is available, add it to the HTML.
          if (!$node
            ->getAttribute('data-entity-uuid') && !empty($entity->uuid)) {
            $node
              ->setAttribute('data-entity-uuid', $entity->uuid);
          }
          $context = entity_embed_get_dom_node_attributes_as_array($node);
          $context += array(
            'data-langcode' => $langcode,
          );
          $entity_output = entity_embed_render_entity($entity_type, $entity, $context);
          $depth--;
        }
        else {
          throw new EntityEmbedEntityNotFoundException(sprintf('Unable to load embedded %s entity %s.', $entity_type, $id));
        }
      } catch (\Exception $e) {
        watchdog_exception('entity_embed', $e);
      }
      entity_embed_replace_dom_node_content($node, $entity_output);
    }
    $result = entity_embed_serialize($dom);
  }
  return $result;
}