You are here

function theme_viewfield_formatter_default in Viewfield 6.2

Same name and namespace in other branches
  1. 6 theme/viewfield.theme.inc \theme_viewfield_formatter_default()
  2. 7.3 viewfield.module \theme_viewfield_formatter_default()
  3. 7.2 viewfield.module \theme_viewfield_formatter_default()

Return a themed view avoiding viewfield recursion.

File

./viewfield.module, line 108
Core functions.

Code

function theme_viewfield_formatter_default($element) {

  // $_viewfield_stack keeps a record of the current node to prevent infinite
  // recursion during the view rendering process.
  global $_viewfield_stack;
  $node = $element['#node'];
  if (!empty($element['#item']['vname']) && !isset($_viewfield_stack[$node->nid])) {

    // Push id of current node unless it's a new node being previewed.
    if ($node->nid) {
      $_viewfield_stack[$node->nid] = $node->nid;
    }
    list($view_name, $display) = explode('|', $element['#item']['vname'], 2);
    $view_args = _viewfield_get_view_args($element['#item']['vargs'], $element['#node']);

    // Render the view like Views would do.
    // @see views_embed_view()
    $view = views_get_view($view_name);
    if ($view && $view
      ->access($display)) {

      // Override the view's path to the current path. Otherwise, exposed views
      // filters would submit to the front page.
      $view->override_path = $_GET['q'];
      $output = $view
        ->preview($display, $view_args);
    }

    // This node is "safe" again.
    if ($node->nid) {
      unset($_viewfield_stack[$node->nid]);
    }

    // Only return an actual view result to not break empty value behavior.
    if (isset($output)) {
      return $output;
    }
  }
}