5 * Content type plugin to allow Views to be exposed as a display type,
6 * leaving most of the configuration on the view.
10 * Implements hook_ctools_content_types()
12 function views_content_views_panes_ctools_content_types() {
14 'title' => t('View panes'),
15 'admin settings' => 'views_content_admin_form',
16 'all contexts' => TRUE,
21 * Return all content types available.
23 function views_content_views_panes_content_type_content_types($plugin) {
25 // It can be fairly intensive to calculate this, so let's cache this in the
26 // cache_views table. The nice thing there is that if views ever change, that
27 // table will always be cleared. Except for the occasional default view, so
28 // we must use the Views caching functions in order to respect Views caching
30 views_include('cache');
31 $data = views_cache_get('views_content_panes', TRUE);
32 if (!empty($data->data)) {
39 $views = views_get_all_views();
41 foreach ($views as $view) {
42 if (!empty($view->disabled)) {
46 $view->init_display();
48 foreach ($view->display as $id => $display) {
49 if (empty($display->handler->panel_pane_display)) {
52 $info = _views_content_panes_content_type($view, $display);
54 $types[$view->name . '-' . $id] = $info;
60 views_cache_set('views_content_panes', $types, TRUE);
67 * Return a single content type.
69 function views_content_views_panes_content_type_content_type($subtype, $plugin) {
70 list($name, $display) = explode('-', $subtype);
71 $view = views_get_view($name);
76 $view->set_display($display);
77 $retval = _views_content_panes_content_type($view, $view->display[$display]);
83 function _views_content_panes_content_type($view, $display) {
84 // Ensure the handler is the right type, as Views will fall back to
85 // the default display if something is broken:
86 if (!is_a($display->handler, 'views_content_plugin_display_panel_pane')) {
90 $title = views_content_get_display_title($view, $display->id);
92 $description = $display->handler->get_option('pane_description');
94 $description = $view->description;
97 $category = $display->handler->get_option('pane_category');
98 if (!$category['name']) {
99 $category['name'] = t('View panes');
102 $icon = 'icon_views_page.png';
106 $arguments = $display->handler->get_argument_input();
107 ctools_include('views');
108 foreach ($arguments as $argument) {
109 $contexts[] = ctools_views_get_argument_context($argument);
112 $allow = $display->handler->get_option('allow');
116 'description' => filter_xss_admin($description),
117 'required context' => $contexts,
118 'category' => array($category['name'], $category['weight']),
119 'no title override' => empty($allow['title_override']),
124 * Output function for the 'views' content type.
126 * Outputs a view based on the module and delta supplied in the configuration.
128 function views_content_views_panes_content_type_render($subtype, $conf, $panel_args, $contexts) {
129 if (!is_array($contexts)) {
130 $contexts = array($contexts);
133 list($name, $display) = explode('-', $subtype);
134 $view = views_get_view($name);
139 $view->set_display($display);
140 views_content_views_panes_add_defaults($conf, $view);
142 if (!$view->display_handler->access($GLOBALS['user']) || empty($view->display_handler->panel_pane_display)) {
146 $view->display_handler->set_pane_conf($conf);
149 $arguments = $view->display_handler->get_option('arguments');
151 $context_keys = isset($conf['context']) ? $conf['context'] : array();
152 foreach ($view->display_handler->get_argument_input() as $id => $argument) {
153 switch ($argument['type']) {
155 $key = array_shift($context_keys);
156 if (isset($contexts[$key])) {
157 if (strpos($argument['context'], '.')) {
158 list($context, $converter) = explode('.', $argument['context'], 2);
159 $args[] = ctools_context_convert_context($contexts[$key], $converter, array('sanitize' => FALSE));
162 $args[] = $contexts[$key]->argument;
166 $args[] = isset($arguments[$id]['exception']['value']) ? $arguments[$id]['exception']['value'] : 'all';
171 $args[] = $argument['fixed'];
175 $args[] = isset($panel_args[$argument['panel']]) ? $panel_args[$argument['panel']] : NULL;
179 $args[] = (isset($conf['arguments'][$id]) && $conf['arguments'][$id] !== '') ? ctools_context_keyword_substitute($conf['arguments'][$id], array(), $contexts) : NULL;
183 // Put in the wildcard.
184 $args[] = isset($arguments[$id]['wildcard']) ? $arguments[$id]['wildcard'] : '*';
190 // views.module knows what to do with NULL (or missing) arguments
196 // remove any trailing NULL arguments as these are non-args:
197 while (count($args) && end($args) === NULL) {
201 $view->set_arguments($args);
203 $allow = $view->display_handler->get_option('allow');
205 if (!empty($conf['path'])) {
206 $conf['path'] = ctools_context_keyword_substitute($conf['path'], array(), $contexts);
208 if ($allow['path_override'] && !empty($conf['path'])) {
209 $view->override_path = $conf['path'];
211 else if ($path = $view->display_handler->get_option('inherit_panels_path')) {
212 $view->override_path = $_GET['q'];
215 $block = new stdClass();
216 $block->module = 'views';
217 $block->delta = $view->name . '-' . $display;
219 if (($allow['link_to_view'] && !empty($conf['link_to_view'])) ||
220 (!$allow['link_to_view'] && $view->display_handler->get_option('link_to_view'))) {
221 $block->title_link = $view->get_url();
225 if ($allow['more_link']) {
226 if (empty($conf['more_link'])) {
227 $view->display_handler->set_option('use_more', FALSE);
230 $view->display_handler->set_option('use_more', TRUE);
231 // make sure the view runs the count query so we know whether or not the
232 // more link applies.
233 $view->get_total_rows = TRUE;
237 if ($allow['items_per_page'] && isset($conf['items_per_page'])) {
238 $view->set_items_per_page($conf['items_per_page']);
241 if ($allow['offset']) {
242 $view->set_offset($conf['offset']);
245 if ($allow['use_pager']) {
246 // Only set use_pager if they differ, this way we can avoid overwriting the
247 // pager type that Views uses.
248 $pager = $view->display_handler->get_option('pager');
249 if ($conf['use_pager'] && ($pager['type'] == 'none' || $pager['type'] == 'some')) {
250 $pager['type'] = 'full';
252 elseif (!$conf['use_pager'] && $pager['type'] != 'none' && $pager['type'] != 'some') {
253 $pager['type'] = $view->get_items_per_page() || !empty($pager['options']['items_per_page']) ? 'some' : 'none';
256 if ($conf['use_pager']) {
257 if (!isset($pager['options']['id']) || (isset($conf['pager_id']) && $pager['options']['id'] != $conf['pager_id'])) {
258 $pager['options']['id'] = (int) $conf['pager_id'];
262 $view->display_handler->set_option('pager', $pager);
265 if ($allow['fields_override']) {
266 if ($conf['fields_override']) {
267 $fields = $view->get_items('field');
268 foreach ($fields as $field => $field_display) {
269 $fields[$field]['exclude'] = empty($conf['fields_override'][$field]);
271 $view->display_handler->set_option('fields', $fields);
276 if ($allow['exposed_form'] && !empty($conf['exposed'])) {
277 foreach ($conf['exposed'] as $filter_name => $filter_value) {
278 if (!is_array($filter_value)) {
279 $conf['exposed'][$filter_name] = ctools_context_keyword_substitute($filter_value, array(), $contexts);
282 $view->set_exposed_input($conf['exposed']);
285 $stored_feeds = drupal_add_feed();
287 $block->content = $view->preview();
288 if (empty($view->result) && !$view->display_handler->get_option('empty') && empty($view->style_plugin->definition['even empty'])) {
292 // Add contextual links to the output.
293 $block = (array) $block;
294 views_add_block_contextual_links($block, $view, $display, 'panel_pane');
295 $block = (object) $block;
297 $block->title = $view->get_title();
299 if (empty($view->total_rows) || $view->total_rows <= $view->get_items_per_page()) {
303 if ((!empty($allow['feed_icons']) && !empty($conf['feed_icons'])) ||
304 (empty($allow['feed_icons']) && $view->display_handler->get_option('feed_icons'))) {
305 $new_feeds = drupal_add_feed();
306 if ($diff = array_diff(array_keys($new_feeds), array_keys($stored_feeds))) {
307 foreach ($diff as $url) {
308 $block->feeds[$url] = $new_feeds[$url];
317 * Add defaults to view pane settings.
318 * This helps cover us if $allow settings changed and there are no actual
319 * settings for a particular item.
321 function views_content_views_panes_add_defaults(&$conf, $view) {
322 $pager = $view->display_handler->get_option('pager');
325 'link_to_view' => $view->display_handler->get_option('link_to_view'),
326 'more_link' => $view->display_handler->get_option('use_more'),
327 'feed_icons' => FALSE,
328 'use_pager' => $pager['type'] != 'none' && $pager['type'] != 'some',
329 'pager_id' => isset($pager['options']['id']) ? $pager['options']['id'] : 0,
330 'items_per_page' => !empty($pager['options']['items_per_page']) ? $pager['options']['items_per_page'] : 10,
331 'offset' => !empty($pager['options']['offset']) ? $pager['options']['offset'] : 0,
332 'path_override' => FALSE,
333 'path' => $view->get_path(),
334 'fields_override' => $view->display_handler->get_option('fields_override'),
339 * Returns an edit form for a block.
341 function views_content_views_panes_content_type_edit_form($form, &$form_state) {
342 $conf = $form_state['conf'];
343 $contexts = $form_state['contexts'];
344 // This allows older content to continue to work, where we used to embed
345 // the display directly.
346 list($name, $display_id) = explode('-', $form_state['subtype_name']);
347 $view = views_get_view($name);
350 $form['markup'] = array('#markup' => t('Broken/missing/deleted view.'));
354 $view->set_display($display_id);
356 // If it couldn't set the display and we got the default display instead,
358 if ($view->current_display == 'default') {
359 $form['markup'] = array('#markup' => t('Broken/missing/deleted view display.'));
363 $allow = $view->display_handler->get_option('allow');
365 // Provide defaults for everything in order to prevent warnings.
366 views_content_views_panes_add_defaults($conf, $view);
368 $form['arguments']['#tree'] = TRUE;
370 foreach ($view->display_handler->get_argument_input() as $id => $argument) {
371 if ($argument['type'] == 'user') {
372 $form['arguments'][$id] = array(
373 '#type' => 'textfield',
374 '#default_value' => isset($conf['arguments'][$id]) ? $conf['arguments'][$id] : '',
375 '#description' => t('You may use keywords for substitutions.'),
376 '#title' => check_plain($argument['label']),
380 if ($allow['link_to_view'] ) {
381 $form['link_to_view'] = array(
382 '#type' => 'checkbox',
383 '#default_value' => isset($conf['link_to_view']) ? $conf['link_to_view'] : $view->display_handler->get_option('link_to_view'),
384 '#title' => t('Link title to page'),
387 if ($allow['more_link']) {
388 $form['more_link'] = array(
389 '#type' => 'checkbox',
390 '#default_value' => isset($conf['more_link']) ? $conf['more_link'] : $view->display_handler->get_option('use_more'),
391 '#description' => t('The text of this link will be "@more". This setting can only be modified on the View configuration.', array('@more' => $view->display_handler->use_more_text())),
392 '#title' => t('Provide a "more" link.'),
396 if (!empty($allow['feed_icons'])) {
397 $form['feed_icons'] = array(
398 '#type' => 'checkbox',
399 '#default_value' => !empty($conf['feed_icons']),
400 '#title' => t('Display feed icons'),
405 if ($allow['fields_override'] && $view->style_plugin->uses_fields()) {
406 $form['fields_override'] = array(
407 '#type' => 'fieldset',
408 '#title' => 'Fields to display',
409 '#collapsible' => TRUE,
412 foreach ($view->display_handler->get_handlers('field') as $field => $handler) {
413 $title = $handler->ui_name();
414 if ($handler->options['label']) {
415 $title .= ' (' . check_plain($handler->options['label']) . ')';
418 $form['fields_override'][$field] = array(
419 '#type' => 'checkbox',
421 '#default_value' => isset($conf['fields_override'][$field]) ? $conf['fields_override'][$field] : !$handler->options['exclude'],
426 ctools_include('dependent');
427 if ($allow['use_pager']) {
428 $form['use_pager'] = array(
429 '#type' => 'checkbox',
430 '#title' => t('Use pager'),
431 '#default_value' => $conf['use_pager'],
432 '#id' => 'use-pager-checkbox',
433 '#prefix' => '<div class="container-inline">',
435 $form['pager_id'] = array(
436 '#type' => 'textfield',
437 '#default_value' => $conf['pager_id'],
438 '#title' => t('Pager ID'),
440 '#id' => 'use-pager-textfield',
441 '#dependency' => array('use-pager-checkbox' => array(1)),
442 '#suffix' => '</div>',
445 if ($allow['items_per_page']) {
446 $form['items_per_page'] = array(
447 '#type' => 'textfield',
448 '#default_value' => $conf['items_per_page'],
449 '#title' => t('Num items'),
451 '#description' => t('Select the number of items to display, or 0 to display all results.'),
454 if ($allow['offset']) {
455 $form['offset'] = array(
456 '#type' => 'textfield',
457 '#default_value' => $conf['offset'],
458 '#title' => t('Offset'),
460 '#description' => t('Enter the number of items to skip; enter 0 to skip no items.'),
463 if ($allow['path_override']) {
464 $form['path'] = array(
465 '#type' => 'textfield',
466 '#default_value' => isset($conf['path']) ? $conf['path'] : $view->get_path(),
467 '#title' => t('Override path'),
469 '#description' => t('If this is set, override the View URL path; this can sometimes be useful to set to the panel URL.'),
471 if (!empty($contexts)) {
472 $form['path']['#description'] .= ' ' . t('You may use substitutions in this path.');
474 $form['contexts'] = array(
475 '#type' => 'fieldset',
476 '#title' => t('Substitutions'),
477 '#collapsible' => TRUE,
478 '#collapsed' => TRUE,
482 foreach ($contexts as $context) {
483 foreach (ctools_context_get_converters('%' . check_plain($context->keyword) . ':', $context) as $keyword => $title) {
485 check_plain($keyword),
486 t('@identifier: @title', array('@title' => $title, '@identifier' => $context->identifier)),
491 $header = array(t('Keyword'), t('Value'));
492 $form['contexts']['context'] = array('#markup' => theme('table', array('header' => $header, 'rows' => $rows)));
496 if (empty($conf['exposed'])) {
497 $conf['exposed'] = array();
500 if ($allow['exposed_form']) {
501 // If the exposed form is part of pane configuration, get the exposed
502 // form re-tool it for our use.
503 $exposed_form_state = array(
505 'display' => &$view->display[$display_id],
508 $view->set_exposed_input($conf['exposed']);
510 if (version_compare(views_api_version(), '3', '>=')) {
511 $exposed_form_state['exposed_form_plugin'] = $view->display_handler->get_plugin('exposed_form');
513 $view->init_handlers();
514 $exposed_form = array();
515 $exposed_form = views_exposed_form($exposed_form, $exposed_form_state);
517 $form['exposed'] = array(
521 foreach ($exposed_form['#info'] as $id => $info) {
522 $form['exposed'][$id] = array(
524 '#id' => 'views-exposed-pane',
527 if (!empty($info['label'])) {
528 $form['exposed'][$id]['#title'] = $info['label'];
531 if (!empty($info['operator']) && !empty($exposed_form[$info['operator']])) {
532 $form['exposed'][$id][$info['operator']] = $exposed_form[$info['operator']];
533 $form['exposed'][$id][$info['operator']]['#parents'] = array('exposed', $info['operator']);
534 $form['exposed'][$id][$info['operator']]['#default_value'] = isset($conf['exposed'][$info['operator']]) ? $conf['exposed'][$info['operator']] : '';
536 $form['exposed'][$id][$info['value']] = $exposed_form[$info['value']];
537 $form['exposed'][$id][$info['value']]['#parents'] = array('exposed', $info['value']);
538 $form['exposed'][$id][$info['value']]['#default_value'] = isset($conf['exposed'][$info['value']]) ? $conf['exposed'][$info['value']] : '';
542 // The exposed sort stuff doesn't fall into $exposed_form['#info'] so we
543 // have to handle it separately.
544 if (isset($exposed_form['sort_by'])) {
545 $form['exposed']['sort_by'] = $exposed_form['sort_by'];
548 if (isset($exposed_form['sort_order'])) {
549 $form['exposed']['sort_order'] = $exposed_form['sort_order'];
552 // Add the view object to the form to allow additional customization
553 $form_state['view'] = $view;
559 * Store form values in $conf.
561 function views_content_views_panes_content_type_edit_form_submit(&$form, &$form_state) {
562 // Copy everything from our defaults.
563 $keys = array('link_to_view', 'more_link', 'feed_icons', 'use_pager',
564 'pager_id', 'items_per_page', 'offset', 'path_override', 'path', 'arguments', 'fields_override', 'exposed');
566 foreach ($keys as $key) {
567 if (isset($form_state['values'][$key])) {
568 $form_state['conf'][$key] = $form_state['values'][$key];
575 * Returns the administrative title for a type.
577 function views_content_views_panes_content_type_admin_title($subtype, $conf, $contexts) {
578 list($name, $display) = explode('-', $subtype);
579 $view = views_get_view($name);
580 if (empty($view) || empty($view->display[$display])) {
581 return t('Deleted/missing view @view', array('@view' => $name));
584 $view->set_display($display);
585 views_content_views_panes_add_defaults($conf, $view);
587 $title = views_content_get_display_title($view, $display);
589 return check_plain($title);
593 * Returns the administrative title for a type.
595 function views_content_views_panes_content_type_admin_info($subtype, $conf, $contexts) {
598 list($view_name, $display_name) = explode('-', $subtype);
599 $view = views_get_view($view_name);
601 if (empty($view) || empty($view->display[$display_name])) {
605 $view->set_display($display_name);
606 views_content_views_panes_add_defaults($conf, $view);
608 // Add arguments first
609 if (!empty($conf['arguments'])) {
610 $keys = array_keys($conf['arguments']);
611 $values = array_values($conf['arguments']);
612 $argument_input = $view->display_handler->get_option('argument_input');
614 foreach ($conf['arguments'] as $key => $value) {
616 $label = $argument_input[$key]['label'];
617 $info[] = $label . ': ' . $value;
622 $block = new stdClass;
624 $block->title = array_shift($info);
626 $info[] = $view->display_handler->get_option('pane_description');
627 $block->content = theme('item_list', array('items' => $info));
630 $block->title = $view->display_handler->get_option('pane_description');
631 $block->content = '';