You are here

function theme_viewfield_formatter_default in Viewfield 6

Same name and namespace in other branches
  1. 6.2 viewfield.module \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

theme/viewfield.theme.inc, line 11
Theme functions.

Code

function theme_viewfield_formatter_default($element) {
  global $_viewfield_stack;
  $node = $element['#node'];

  // For safety's sake, we can only display 2 levels of viewfields.
  if (!isset($_viewfield_stack[$node->nid]) && count($_viewfield_stack) <= 2) {
    list($view_name, $display) = explode('|', $element['#item']['vname'], 2);
    $view_args = _viewfield_get_view_args($element['#item']['token_enabled'], $element['#item']['vargs'], $element['#node']);

    // Prevent infinite recursion.
    if ($node->nid) {
      $_viewfield_stack[$node->nid] = $node->nid;
    }

    // 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;
    }
  }
}