better_exposed_filters.theme in Better Exposed Filters 8.3
Same filename and directory in other branches
Provides theming functions to display exposed forms using different interfaces.
File
better_exposed_filters.themeView source
<?php
/**
* @file
* Provides theming functions to display exposed forms using different
* interfaces.
*/
/**
* Themes a select element as checkboxes enclosed in a details element.
*
* @param array $vars
* An array of arrays, the 'element' item holds the properties of the element
*
* @return string
* HTML representing the form element.
*/
function theme_select_as_checkboxes_details($vars) {
// Merge incoming element with some default values. Prevents a lot of this.
// $foo = isset($bar) ? $bar : $bar_default;
$element = array_merge(array(
'#bef_title' => '',
'#bef_description' => '',
'#bef_operator' => array(),
), $vars['element']);
$details = array(
'#title' => $element['#bef_title'],
'#description' => $element['#bef_description'],
'#attributes' => array(
'class' => array(
'bef-select-as-checkboxes-details',
),
),
);
if (empty($element['#value'])) {
// Using the FAPI #collapsible and #collapsed attribute doesn't work here
// TODO: not sure why...
$details['#attributes']['class'][] = 'collapsed';
}
// We rendered the description as part of the details element, don't render
// it again along with the checkboxes.
unset($element['#bef_description']);
$children = '';
if (!empty($element['#bef_operator'])) {
// Put an exposed operator inside the details element.
$children = drupal_render($element['#bef_operator']);
}
// Render the checkboxes.
// @TODO: Render in a template
$children .= theme('select_as_checkboxes', array(
'element' => $element,
));
$details['#children'] = $children;
// @TODO: Render in a template
return theme('fieldset', array(
'element' => $details,
));
}
/**
* Themes a select element as a series of hidden fields.
*
* @see http://api.drupal.org/api/function/theme_select/7
*
* @param array $vars
* An array of arrays, the 'element' item holds the properties of the element.
*
* @return string
* HTML representing the form element.
*/
function theme_select_as_hidden($vars) {
$element = $vars['element'];
$output = '';
$selected_options = empty($element['#value']) ? $element['#default_value'] : $element['#value'];
$properties = array(
'title' => isset($element['#title']) ? $element['#title'] : '',
'description' => isset($element['#bef_description']) ? $element['#bef_description'] : '',
'required' => FALSE,
);
foreach ($element['#options'] as $option => $elem) {
// Check for Taxonomy-based filters.
if (is_object($elem)) {
$slice = array_slice($elem->option, 0, 1, TRUE);
$option = key($slice);
$elem = current($slice);
}
// Check for optgroups. Put subelements in the $element_set array and add a
// group heading. Otherwise, just add the element to the set.
$element_set = array();
if (is_array($elem)) {
$element_set = $elem;
}
else {
$element_set[$option] = $elem;
}
foreach ($element_set as $key => $value) {
// Only render fields for selected values -- no selected values renders
// zero fields.
if (array_search($key, $selected_options) !== FALSE) {
// Custom ID for each hidden field based on the <select>'s original ID.
$id = drupal_html_id($element['#id'] . '-' . $key);
// Very similar to theme_hidden()
// (http://api.drupal.org/api/function/theme_hidden/7).
// @TODO: Render in a template
$hidden = '<input type="hidden" ' . 'name="' . filter_xss($element['#name']) . '[]" ' . 'id="' . $id . '" ' . 'value="' . check_plain($key) . '" ' . drupal_attributes($element['#attributes']) . ' />';
// @TODO: Render in a template
$output .= theme('form_element', array(
'element' => array_merge($properties, array(
'#id' => $id,
'#children' => $hidden,
)),
));
}
}
}
return $output;
}
/**
* Themes a select element as radio buttons enclosed in a details element.
*
* @param array $vars
* An array of arrays, the 'element' item holds the properties of the element.
*
* @return string
* HTML representing the form element.
*/
function theme_select_as_radios_details($vars) {
// Merge incoming element with some default values. Prevents a lot of this.
// $foo = isset($bar) ? $bar : $bar_default;
$element = array_merge(array(
'#bef_title' => '',
'#bef_description' => '',
'#bef_operator' => array(),
), $vars['element']);
// The "all" option is the first in the list. If the selected radio button is
// the all option, then leave the details element collapsed. Otherwise,
// render it opened.
$keys = array_keys($element['#options']);
$all = array_shift($keys);
$details = array(
'#title' => $element['#bef_title'],
'#description' => $element['#bef_description'],
'#attributes' => array(
'class' => array(
'bef-select-as-checkboxes-details',
),
),
);
// We rendered the description as part of the details element, don't render
// it again along with the checkboxes.
unset($element['#bef_description']);
$children = '';
if (!empty($element['#bef_operator'])) {
// Put an exposed operator inside the fieldset.
$children = drupal_render($element['#bef_operator']);
}
// Render the radio buttons.
// @TODO: render in a template
$children .= theme('select_as_radios', $element);
$details['#children'] = $children;
// @TODO: render in a template
return theme('fieldset', array(
'element' => $details,
));
}
/**
* Themes a taxonomy-based exposed filter as a nested unordered list.
*
* Note: this routine depends on the '-' char prefixed on the term names by
* Views to determine depth.
*
* @param array $vars
* An array of arrays, the 'element' item holds the properties of the element.
*
* @return string
* Nested, unordered list of filter options
*/
function theme_select_as_tree($vars) {
$element = $vars['element'];
// The selected keys from #options.
$selected_options = empty($element['#value']) ? $element['#default_value'] : $element['#value'];
// Build a bunch of nested unordered lists to represent the hierarchy based
// on the '-' prefix added by Views or optgroup structure.
$output = '<ul class="bef-tree">';
$curr_depth = 0;
foreach ($element['#options'] as $option_value => $option_label) {
// Check for Taxonomy-based filters.
if (is_object($option_label)) {
$slice = array_slice($option_label->option, 0, 1, TRUE);
$option_value = key($slice);
$option_label = current($slice);
}
// Check for optgroups -- which is basically a two-level deep tree.
if (is_array($option_label)) {
// TODO:
}
else {
// Build hierarchy based on prefixed '-' on the element label.
if (t('- Any -') == $option_label) {
$depth = 0;
}
else {
preg_match('/^(-*).*$/', $option_label, $matches);
$depth = strlen($matches[1]);
$option_label = ltrim($option_label, '-');
}
// Build either checkboxes or radio buttons, depending on Views' settings.
$html = '';
if (!empty($element['#multiple'])) {
$html = bef_checkbox($element, $option_value, $option_label, array_search($option_value, $selected_options) !== FALSE);
}
else {
$element[$option_value]['#title'] = $option_label;
// @TODO: Render in a template
$element[$option_value]['#children'] = theme('radio', array(
'element' => $element[$option_value],
));
// @TODO: Render in a template
$html .= theme('form_element', array(
'element' => $element[$option_value],
));
}
if ($depth > $curr_depth) {
// We've moved down a level: create a new nested <ul>.
// TODO: Is there is a way to jump more than one level deeper at a time?
// I don't think so...
$output .= "<ul class='bef-tree-child bef-tree-depth-{$depth}'><li>{$html}";
$curr_depth = $depth;
}
elseif ($depth < $curr_depth) {
// We've moved up a level: finish previous <ul> and <li> tags, once for
// each level, since we can jump multiple levels up at a time.
while ($depth < $curr_depth) {
$output .= '</li></ul>';
$curr_depth--;
}
$output .= "</li><li>{$html}";
}
else {
// Remain at same level as previous entry. No </li> needed if we're at
// the top level.
if (0 == $curr_depth) {
$output .= "<li>{$html}";
}
else {
$output .= "</li><li>{$html}";
}
}
}
}
// foreach ($element['#options'] as $option_value => $option_label)
if (!$curr_depth) {
// Close last <li> tag.
$output .= '</li>';
}
else {
// Finish closing <ul> and <li> tags.
while ($curr_depth) {
$curr_depth--;
$output .= '</li></ul></li>';
}
}
// Close the opening <ul class="bef-tree"> tag.
$output .= '</ul>';
// Add exposed filter description.
$description = '';
if (!empty($element['#bef_description'])) {
$description = '<div class="description">' . $element['#bef_description'] . '</div>';
}
// Add the select all/none option, if needed.
if (!empty($element['#bef_select_all_none'])) {
if (empty($element['#attributes']['class'])) {
$element['#attributes']['class'] = array();
}
$element['#attributes']['class'][] = 'bef-select-all-none';
}
// Add the select all/none nested option, if needed.
if (!empty($element['#bef_select_all_none_nested'])) {
if (empty($element['#attributes']['class'])) {
$element['#attributes']['class'] = array();
}
$element['#attributes']['class'][] = 'bef-select-all-none-nested';
}
// Name and multiple attributes are not valid for <div>'s.
if (isset($element['#attributes']['name'])) {
unset($element['#attributes']['name']);
}
if (isset($element['#attributes']['multiple'])) {
unset($element['#attributes']['multiple']);
}
return '<div' . drupal_attributes($element['#attributes']) . ">{$description}{$output}</div>";
}
/**
* Themes a select drop-down as a collection of links.
*
* @see http://api.drupal.org/api/function/theme_select/7
*
* @param array $vars
* An array of arrays, the 'element' item holds the properties of the element.
*
* @return string
* HTML representing the form element.
*/
function theme_select_as_links($vars) {
$element = $vars['element'];
$output = '';
$name = $element['#name'];
// Collect selected values so we can properly style the links later.
$selected_options = array();
if (empty($element['#value'])) {
if (!empty($element['#default_values'])) {
$selected_options[] = $element['#default_values'];
}
}
else {
$selected_options[] = $element['#value'];
}
// Add to the selected options specified by Views whatever options are in the
// URL query string, but only for this filter.
$urllist = parse_url(request_uri());
if (isset($urllist['query'])) {
$query = array();
parse_str(urldecode($urllist['query']), $query);
foreach ($query as $key => $value) {
if ($key != $name) {
continue;
}
if (is_array($value)) {
// This filter allows multiple selections, so put each one on the
// selected_options array.
foreach ($value as $option) {
$selected_options[] = $option;
}
}
else {
$selected_options[] = $value;
}
}
}
// Clean incoming values to prevent XSS attacks.
if (is_array($element['#value'])) {
foreach ($element['#value'] as $index => $item) {
unset($element['#value'][$index]);
$element['#value'][filter_xss($index)] = filter_xss($item);
}
}
elseif (is_string($element['#value'])) {
$element['#value'] = filter_xss($element['#value']);
}
// Go through each filter option and build the appropriate link or plain text.
foreach ($element['#options'] as $option => $elem) {
// Check for Taxonomy-based filters.
if (is_object($elem)) {
$slice = array_slice($elem->option, 0, 1, TRUE);
$option = key($slice);
$elem = current($slice);
}
// Check for optgroups. Put subelements in the $element_set array and add
// a group heading. Otherwise, just add the element to the set.
$element_set = array();
if (is_array($elem)) {
$element_set = $elem;
}
else {
$element_set[$option] = $elem;
}
$links = array();
$multiple = !empty($element['#multiple']);
// If we're in an exposed block, we'll get passed a path to use for the
// Views results page.
$path = '';
if (!empty($element['#bef_path'])) {
$path = $element['#bef_path'];
}
foreach ($element_set as $key => $value) {
// Custom ID for each link based on the <select>'s original ID.
$id = drupal_html_id($element['#id'] . '-' . $key);
$elem = array(
'#id' => $id,
'#markup' => '',
'#type' => 'bef-link',
'#name' => $id,
);
if (array_search($key, $selected_options) === FALSE) {
$elem['#children'] = l($value, bef_replace_query_string_arg($name, $key, $multiple, FALSE, $path));
// @TODO: Render in a template
$output .= theme('form_element', array(
'element' => $elem,
));
}
else {
$elem['#children'] = l($value, bef_replace_query_string_arg($name, $key, $multiple, TRUE, $path));
_form_set_class($elem, array(
'bef-select-as-links-selected',
));
// @TODO: Render in a template
$output .= str_replace('form-item', 'form-item selected', theme('form_element', array(
'element' => $elem,
)));
}
}
}
$properties = array(
'#description' => isset($element['#bef_description']) ? $element['#bef_description'] : '',
'#children' => $output,
);
// @TODO: Render in a template
$output = '<div class="bef-select-as-links">';
$output .= theme('form_element', array(
'element' => $properties,
));
if (!empty($element['#value'])) {
if (is_array($element['#value'])) {
foreach ($element['#value'] as $value) {
$output .= '<input type="hidden" name="' . $name . '[]" value="' . $value . '" />';
}
}
else {
$output .= '<input type="hidden" name="' . $name . '" value="' . $element['#value'] . '" />';
}
}
$output .= '</div>';
return $output;
}
/**
* Themes some exposed form elements in a details element.
*
* @param array $vars
* An array of arrays, the 'element' item holds the properties of the element.
*
* @return string
* HTML to render the form element.
*/
function theme_secondary_exposed_elements($vars) {
$element = $vars['element'];
// TODO: put HTML in a template
$output = '<div class="bef-secondary-options">';
foreach (element_children($element) as $id) {
$output .= drupal_render($element[$id]);
}
$output .= '</div>';
return $output;
}
/*
*
* Helper functions
*
*/
/**
* Build a BEF checkbox.
*
* @see http://api.drupal.org/api/function/theme_checkbox/7
*
* @param array $element
* Original <select> element generated by Views.
* @param string $value
* Return value of this checkbox option.
* @param string $label
* Label of this checkbox option.
* @param bool $selected
* Checked or not.
*
* @return [type]
* HTML to render a checkbox.
*/
function bef_checkbox($element, $value, $label, $selected) {
$value = check_plain($value);
$label = filter_xss_admin($label);
$id = drupal_html_id($element['#id'] . '-' . $value);
// Custom ID for each checkbox based on the <select>'s original ID.
$properties = array(
'#required' => FALSE,
'#id' => $id,
'#type' => 'bef-checkbox',
'#name' => $id,
);
// Prevent the select-all-none class from cascading to all checkboxes.
if (!empty($element['#attributes']['class']) && FALSE !== ($key = array_search('bef-select-all-none', $element['#attributes']['class']))) {
unset($element['#attributes']['class'][$key]);
}
// Unset the name attribute as we are setting it manually.
unset($element['#attributes']['name']);
// Unset the multiple attribute as it doesn't apply for checkboxes.
unset($element['#attributes']['multiple']);
// @TODO: Render in a template
$checkbox = '<input type="checkbox" ' . 'name="' . $element['#name'] . '[]" ' . 'id="' . $id . '" ' . 'value="' . $value . '" ' . ($selected ? 'checked="checked" ' : '') . drupal_attributes($element['#attributes']) . ' />';
$properties['#children'] = "{$checkbox} <label class='option' for='{$id}'>{$label}</label>";
$output = theme('form_element', array(
'element' => $properties,
));
return $output;
}
/**
* Replaces/adds a given query string argument to the current URL.
*
* @param string $key
* Query string key (argument).
* @param string $value
* Query string value.
* @param bool $multiple
* (optional) TRUE if this key/value pair allows multiple values.
* @param bool $remove
* (optional) TRUE if this key/value should be a link to remove/unset the
* filter.
* @param string $path
* (optional) Use this specify the View results page when the exposed form
* is displayed as a block and may be a different URL from the results.
* Defaults to the current path if unspecified.
*
* @return string
* URL.
*/
function bef_replace_query_string_arg($key, $value, $multiple = FALSE, $remove = FALSE, $path = '') {
if (!$path) {
$path = implode('/', arg());
}
// Prevents us from having to check for each index from parse_url that we may
// use.
$urllist = array(
'path' => '',
'fragment' => '',
'query' => '',
);
$urllist = array_merge($urllist, parse_url(request_uri()));
$fragment = urldecode($urllist['fragment']);
$query = array();
parse_str(urldecode($urllist['query']), $query);
if (isset($query[$key]) && is_array($query[$key])) {
// Multiple values allowed for this existing key.
if ($remove && ($key_remove = array_search($value, $query[$key])) !== FALSE) {
unset($query[$key][$key_remove]);
}
else {
$query[$key][] = $value;
}
}
else {
// Create a new key.
if ($multiple && !$remove) {
$query[$key] = array(
$value,
);
}
elseif (!$remove) {
$query[$key] = $value;
}
}
return url($path, array(
'query' => $query,
'fragment' => $fragment,
'absolute' => TRUE,
));
}
Functions
Name![]() |
Description |
---|---|
bef_checkbox | Build a BEF checkbox. |
bef_replace_query_string_arg | Replaces/adds a given query string argument to the current URL. |
theme_secondary_exposed_elements | Themes some exposed form elements in a details element. |
theme_select_as_checkboxes_details | Themes a select element as checkboxes enclosed in a details element. |
theme_select_as_hidden | Themes a select element as a series of hidden fields. |
theme_select_as_links | Themes a select drop-down as a collection of links. |
theme_select_as_radios_details | Themes a select element as radio buttons enclosed in a details element. |
theme_select_as_tree | Themes a taxonomy-based exposed filter as a nested unordered list. |