You are here

function _node_embed_replacements in Node Embed 7

Provides the replacement html to be rendered in place of the embed code.

Parameters

array $matches: Embedded code, numeric node id and parameters that has been captured by preg_replace_callback.

Return value

string The rendered HTML replacing the embed code. If embedding fails an empty string will be returned for users without update access or else the embed code.

1 string reference to '_node_embed_replacements'
node_embed_filter_node_embed_process in ./node_embed.module
Process callback for hook_filter.

File

./node_embed.module, line 58
This module defines an input filter for taking an embed code syntax ([[nid: ###]]) and removing the embed code and replacing with a rendered node view at that position in the text field.

Code

function _node_embed_replacements($matches) {
  $stack =& drupal_static(__FUNCTION__, array());

  // Add the node page to the stack.
  if (empty($stack) && ($node_page = menu_get_object('node'))) {
    $stack[$node_page->nid] = $node_page;
  }

  // Check if node isn't already embedded.
  if (array_key_exists($matches[1], $stack)) {
    return _node_embed_user_feedback(end($stack), $matches, 'loop');
  }

  // Check if the node is a real node.
  if (!($node = node_load($matches[1]))) {
    return _node_embed_user_feedback(end($stack), $matches, 'invalid');
  }

  // Add to stack.
  $stack[$node->nid] = $node;

  // Check if node can be viewed by the user.
  if (!node_access('view', $node)) {
    return _node_embed_user_feedback(end($stack), $matches, 'access');
  }

  // Warn about an unpublished node.
  if ($node->status == NODE_NOT_PUBLISHED) {
    _node_embed_user_feedback($node, $matches, 'unpublished');
  }
  $node->node_embed_parameters = array();
  if (isset($matches[2]) && trim($matches[2]) != '') {
    parse_str(trim(str_replace(' ', ' ', $matches[2])), $node->node_embed_parameters);
  }
  if (!isset($node->node_embed_parameters['view_mode'])) {
    $node->node_embed_parameters['view_mode'] = 'node_embed';
  }
  $view = node_view($node, $node->node_embed_parameters['view_mode']);
  $render = drupal_render($view);

  // Node without any problems, remove it from stack.
  unset($stack[$node->nid]);
  return $render;
}