function global_filter_create_widget in Views Global Filter 8
Same name and namespace in other branches
- 7 global_filter.widgets.inc \global_filter_create_widget()
Based on the requested or field-implied widget.
There are three main cases: o global filter driven by a view: user may request any list widget o global filter driven by a node property, search term: textfield o global filter driven by a field: widget default may be overriden by user
Parameters
int $filter_key: 1, 2, 3..
array $form: the form that will be added to, cannot be NULL
array $form_state: the form state
1 call to global_filter_create_widget()
- global_filter_submission_form in ./
global_filter.widgets.inc - Creates the global selector widgets, e.g. drop-down, radio-boxes, links...
File
Code
function global_filter_create_widget($filter_key, &$form, &$form_state) {
$name = $form_state['global_filters'][$filter_key]['name'];
$widget_label = $form_state['global_filters'][$filter_key]['label'];
$option_all_text = global_filter_get_parameter($filter_key, 'option_all_text');
$options = empty($option_all_text) ? array(
'' => GLOBAL_FILTER_DEF_OPTION_ALL_TEXT,
) : ($option_all_text == '<none>' ? array() : array(
'' => $option_all_text,
));
// Establish the widget type and create widget form accordingly.
$exceptions = array(
GLOBAL_FILTER_VIEWS_PROXIMITY_FIELD,
GLOBAL_FILTER_VIEWS_SEARCH_TERMS_FIELD,
GLOBAL_FILTER_VIEWS_SEARCH_API_FULLTEXT,
);
if (strpos($name, 'view') === 0) {
$requested_widget = $form_state['global_filters'][$filter_key]['widget'];
if (empty($requested_widget) || $requested_widget == 'default') {
$requested_widget = 'select';
}
$view_name = drupal_substr($name, 5);
global_filter_add_view_results($options, $view_name);
}
elseif (in_array($name, $exceptions) || in_array($name, array_keys(global_filter_get_node_properties()))) {
$requested_widget = $form_state['global_filters'][$filter_key]['widget'];
if ($requested_widget != 'proximity' && $requested_widget != 'range') {
$requested_widget = 'textfield';
}
}
else {
// Probably a field widget.
if ($name == GLOBAL_FILTER_VIEWS_COUNTRY_FIELD) {
$requested_widget = 'select';
if (global_filter_get_module_parameter('take_countries_from_location_module', TRUE) && module_exists('location')) {
// Assemble country list based on the 2-letter country codes in the
// {location} table.
$countries = array();
$result = db_query('SELECT DISTINCT country FROM {location}');
foreach ($result as $country) {
$countries[$country->country] = location_country_name($country->country);
}
}
else {
// Use core's list. Note: country codes will be in uppercase.
include_once DRUPAL_ROOT . '/includes/locale.inc';
$countries = country_get_list();
}
natcasesort($countries);
$options = array_merge($options, $countries);
}
else {
// Field-based widget.
$field = field_info_field($name);
if (!$field) {
if ($name) {
drupal_set_message(t('The field %name used for filter #@filter does not exist. Please re-configure the associated Global Filter block.', array(
'%name' => $name,
'@filter' => $filter_key,
)), 'error');
}
return;
}
$instances = global_filter_get_field_instances($name);
if (empty($instances)) {
global_filter_debug(t('Global filter: no instances found of field %name', array(
'%name' => $name,
)));
return;
}
if (!($requested_widget = $form_state['global_filters'][$filter_key]['widget'])) {
$requested_widget = 'default';
}
foreach ($instances as $instance) {
// If there are multiple widget instances, pick the one requested.
if ('default_' . $instance['widget']['type'] == $requested_widget) {
break;
}
}
// Starts with 'default' or 'default_'.
if (strpos($requested_widget, 'default') === 0 || strpos($instance['widget']['type'], 'date') === 0 && $requested_widget != 'textfield') {
// Inherit native widget and set back on form.
$form_state['global_filters'][$filter_key]['widget'] = $instance['widget']['type'];
global_filter_debug(t('Global Filter @name (on %bundle) uses %widget widget', array(
'%bundle' => $instance['bundle'],
'@name' => $name,
'%widget' => $instance['widget']['type'],
)));
if (!empty($widget_label)) {
// Also field_default_form() does check_plain().
$instance['label'] = $widget_label == '<none>' ? NULL : $widget_label;
}
global_filter_create_field_instance_widget($option_all_text, $field, $instance, $form, $form_state);
$lang = $form_state['language'];
// Don't want to see asterisk in filter.
$form[$name][$lang]['#required'] = FALSE;
if (empty($form[$name][$lang]['#title'])) {
// Remove empty title so that it won't take up any screen space.
unset($form[$name][$lang]['#title']);
}
// Repeat the above for children and grand-children, if any.
foreach (element_children($form[$name][$lang]) as $key0) {
$form[$name][$lang][$key0]['#required'] = FALSE;
if (empty($form[$name][$lang][$key0]['#title'])) {
unset($form[$name][$lang][$key0]['#title']);
}
foreach (element_children($form[$name][$lang][$key0]) as $key) {
$form[$name][$lang][$key0][$key]['#required'] = FALSE;
if (empty($form[$name][$lang][$key0][$key]['#title'])) {
unset($form[$name][$lang][$key0][$key]['#title']);
}
}
}
return;
}
// Simple or links widget, load up the $options.
if ($field['type'] == 'taxonomy_term_reference') {
$vocabulary_name = $field['settings']['allowed_values'][0]['vocabulary'];
// Only show hierarchy depth indicators for select and multi-select.
$show_depth = drupal_substr($requested_widget, -6) == 'select';
_global_filter_add_terms($options, $vocabulary_name, $show_depth);
}
elseif (!empty($field['settings']['allowed_values'])) {
foreach (list_allowed_values($field) as $value => $label) {
$options[$value] = $label;
}
}
}
}
global_filter_debug(t('Global Filter @name uses %widget widget', array(
'@name' => $name,
'%widget' => $requested_widget,
)));
$form_state['global_filters'][$filter_key]['widget'] = $requested_widget;
switch ($requested_widget) {
case 'links':
global_filter_create_links_widget($filter_key, $options, $form, $form_state);
break;
case 'range':
global_filter_create_range_widget($filter_key, $form, $form_state);
break;
case 'proximity':
global_filter_create_proximity_widget($filter_key, $form, $form_state);
break;
default:
global_filter_create_simple_widget($filter_key, $options, $form, $form_state);
}
if (!empty($widget_label) && $widget_label != '<none>') {
$form[$name]['#title'] = check_plain($widget_label);
}
}