You are here

function views_content_views_panes_content_type_render in Chaos Tool Suite (ctools) 6

Same name and namespace in other branches
  1. 7 views_content/plugins/content_types/views_panes.inc \views_content_views_panes_content_type_render()

Output function for the 'views' content type.

Outputs a view based on the module and delta supplied in the configuration.

File

views_content/plugins/content_types/views_panes.inc, line 141
Content type plugin to allow Views to be exposed as a display type, leaving most of the configuration on the view.

Code

function views_content_views_panes_content_type_render($subtype, $conf, $panel_args, $contexts) {
  if (!is_array($contexts)) {
    $contexts = array(
      $contexts,
    );
  }
  list($name, $display) = explode('-', $subtype);
  $view = views_get_view($name);
  if (empty($view)) {
    return;
  }
  $view
    ->set_display($display);
  if (!$view->display_handler
    ->access($GLOBALS['user']) || !$view->display_handler->panel_pane_display) {
    return;
  }
  $view->display_handler
    ->set_pane_conf($conf);
  $args = array();
  $arguments = $view->display_handler
    ->get_option('arguments');
  $context_keys = array_keys($contexts);
  foreach ($view->display_handler
    ->get_argument_input() as $id => $argument) {
    switch ($argument['type']) {
      case 'context':
        $key = array_shift($context_keys);
        if (isset($contexts[$key])) {
          if (strpos($argument['context'], '.')) {
            list($context, $converter) = explode('.', $argument['context'], 2);
            $args[] = ctools_context_convert_context($contexts[$key], $converter);
          }
          else {
            $args[] = $contexts[$key]->argument;
          }
        }
        break;
      case 'fixed':
        $args[] = $argument['fixed'];
        break;
      case 'panel':
        $args[] = isset($panel_args[$argument['panel']]) ? $panel_args[$argument['panel']] : NULL;
        break;
      case 'user':
        $args[] = isset($conf['arguments'][$id]) && $conf['arguments'][$id] !== '' ? $conf['arguments'][$id] : NULL;
        break;
      case 'wildcard':

        // Put in the wildcard.
        $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
        break;
      case 'none':
      default:

        // Put in NULL.
        // views.module knows what to do with NULL (or missing) arguments
        $args[] = NULL;
        break;
    }
  }

  // remove any trailing NULL arguments as these are non-args:
  while (count($args) && end($args) === NULL) {
    array_pop($args);
  }
  $view
    ->set_arguments($args);
  $allow = $view->display_handler
    ->get_option('allow');
  if (!empty($conf['path'])) {
    $conf['path'] = ctools_context_keyword_substitute($conf['path'], array(), $contexts);
  }
  if ($allow['path_override'] && !empty($conf['path'])) {
    $view->override_path = $conf['path'];
  }
  else {
    if ($path = $view->display_handler
      ->get_option('inherit_panels_path')) {
      $view->override_path = $_GET['q'];
    }
  }
  $block = new stdClass();
  $block->module = 'views';
  $block->delta = $view->name . $display;
  if ($allow['link_to_view'] && !empty($conf['link_to_view']) || !$allow['link_to_view'] && $view->display_handler
    ->get_option('link_to_view')) {
    $block->title_link = $view
      ->get_url();
  }

  // more link
  if ($allow['more_link']) {
    if (empty($conf['more_link'])) {
      $view->display_handler
        ->set_option('use_more', FALSE);
    }
    else {
      $view->display_handler
        ->set_option('use_more', TRUE);

      // make sure the view runs the count query so we know whether or not the
      // more link applies.
      $view->get_total_rows = TRUE;
    }
  }
  if ($allow['items_per_page'] && isset($conf['items_per_page'])) {
    $view->display_handler
      ->set_option('items_per_page', $conf['items_per_page']);

    // And here too, which works in Views 3 where the above does not.
    $view
      ->set_items_per_page($conf['items_per_page']);
  }
  if ($allow['offset']) {
    $view->display_handler
      ->set_option('offset', $conf['offset']);
    $view
      ->set_offset($conf['offset']);
  }
  if ($allow['use_pager']) {

    // Only set use_pager if they differ, this way we can avoid overwriting the
    // pager type that Views uses.
    // Views 3 version
    if (method_exists($view, 'init_pager')) {
      $pager = $view->display_handler
        ->get_option('pager');
      if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
        $pager['type'] = 'full';
      }
      elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
        $pager['type'] = $view
          ->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
      }
      if ($conf['use_pager']) {
        if (!isset($pager['options']['id']) || $pager['options']['id'] != $conf['pager_id']) {
          $pager['options']['id'] = $conf['pager_id'];
        }
      }
      $view->display_handler
        ->set_option('pager', $pager);
    }
    else {
      if (!$view->display_handler
        ->get_option('use_pager') || empty($conf['use_pager'])) {
        $view->display_handler
          ->set_option('use_pager', $conf['use_pager']);
      }
      $view->display_handler
        ->set_option('pager_element', $conf['pager_id']);
    }
  }
  if ($allow['fields_override']) {
    if ($conf['fields_override']) {
      $fields = $view
        ->get_items('field');
      foreach ($fields as $field => $display) {
        $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
      }
      $view->display_handler
        ->set_option('fields', $fields);
    }
  }
  if ($allow['exposed_form'] && !empty($conf['exposed'])) {
    $view
      ->set_exposed_input($conf['exposed']);
  }
  $stored_feeds = drupal_add_feed();
  $block->content = $view
    ->preview();
  if (empty($view->result) && !$view->display_handler
    ->get_option('empty') && empty($view->style_plugin->definition['even empty'])) {
    return;
  }
  $block->title = $view
    ->get_title();
  if (empty($view->total_rows) || $view->total_rows <= $view->display_handler
    ->get_option('items_per_page')) {
    unset($block->more);
  }
  if (!empty($allow['feed_icons']) && !empty($conf['feed_icons']) || empty($allow['feed_icons']) && $view->display_handler
    ->get_option('feed_icons')) {
    $new_feeds = drupal_add_feed();
    if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
      foreach ($diff as $url) {
        $block->feeds[$url] = $new_feeds[$url];
      }
    }
  }
  return $block;
}