Skip to content

Instantly share code, notes, and snippets.

@lmeurs
Last active December 2, 2015 07:05
Show Gist options
  • Save lmeurs/9257b057b28cfd5bd4b4 to your computer and use it in GitHub Desktop.
Save lmeurs/9257b057b28cfd5bd4b4 to your computer and use it in GitHub Desktop.
Custom views_handler_area to allow contextual filters for view areas, see issue at https://www.drupal.org/node/2407687.
<?php
/**
* @file
* Definition of views_handler_area_view.
*/
/**
* Views area handlers. Insert a view inside of an area.
*
* @ingroup views_area_handlers
*/
class viewsviewareafilters_handler_area_view extends views_handler_area {
function option_definition() {
$options = parent::option_definition();
$options['view_to_insert'] = array('default' => '');
$options['inherit_arguments'] = array('default' => FALSE, 'bool' => TRUE);
$options['custom_arguments'] = array('default' => '');
return $options;
}
/**
* Default options form that provides the label widget that all fields
* should have.
*/
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$view_display = $this->view->name . ':' . $this->view->current_display;
$options = array('' => t('-Select-'));
$options += views_get_views_as_options(FALSE, 'all', $view_display, FALSE, TRUE);
$form['view_to_insert'] = array(
'#type' => 'select',
'#title' => t('View to insert'),
'#default_value' => $this->options['view_to_insert'],
'#description' => t('The view to insert into this area.'),
'#options' => $options,
);
$form['inherit_arguments'] = array(
'#type' => 'checkbox',
'#title' => t('Inherit contextual filters'),
'#default_value' => $this->options['inherit_arguments'],
'#description' => t('If checked, this view will receive the same contextual filters as its parent.'),
);
// Add extra text field for custom arguments.
$form['custom_arguments'] = array(
'#type' => 'textfield',
'#title' => t('Custom contextual filters'),
'#default_value' => $this->options['custom_arguments'],
'#description' => t("Separate contextual filter values with a \"/\". For example, %example.", array('%example' => '40/12/10')) . ' ' . t('Custom values will be merged with optionally inherited values, leave a custom value blank to inherit the value. For example, %example.', array('%example' => '40//10')),
);
}
/**
* Render the area
*/
function render($empty = FALSE) {
if (!empty($this->options['view_to_insert'])) {
list($view_name, $display_id) = explode(':', $this->options['view_to_insert']);
$view = views_get_view($view_name);
if (empty($view) || !$view->access($display_id)) {
return;
}
$view->set_display($display_id);
// Avoid recursion
$view->parent_views += $this->view->parent_views;
$view->parent_views[] = "$view_name:$display_id";
// Check if the view is part of the parent views of this view
$search = "$view_name:$display_id";
if (in_array($search, $this->view->parent_views)) {
drupal_set_message(t("Recursion detected in view @view display @display.", array('@view' => $view_name, '@display' => $display_id)), 'error');
}
elseif (!$empty || $this->options['empty']) {
$arguments = array();
// Inherit arguments from parent view.
if (!empty($this->options['inherit_arguments']) && !empty($this->view->args)) {
$arguments = $this->view->args;
}
// If custom arguments have been defined.
if (!empty($this->options['custom_arguments'])) {
// Add arguments to array manually so we can skip empty arguments to
// not overwrite optionally inherited arguments.
foreach (explode('/', $this->options['custom_arguments']) as $i => $argument) {
// Only set argument if it has an actual value.
if ($argument !== '') {
$arguments[$i] = $argument;
}
}
}
return $view->preview($display_id, $arguments);
}
}
return '';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment