| 84 |
|
|
| 85 |
function views_checkboxes_form_alter($form_id, &$form) { |
function views_checkboxes_form_alter($form_id, &$form) { |
| 86 |
|
|
| 87 |
if (isset($form['view']['#value']->exposed_filter)) { |
// Views which have exposed filters get a $form_id of 'views_filters' |
| 88 |
// view filter forms don't seem to have a form_id, so I'm using this conditional to identify them. |
if ($form_id != 'views_filters') return; // Not a Views filter, quick exit |
| 89 |
|
|
| 90 |
$view = $form['view']['#value']; |
// --- Remove this section later --- |
| 91 |
$limit = variable_get('views_checkboxes_limit', 5); |
// My tests indicate that the following tests aren't necessary because |
| 92 |
|
// there are *always* filters if we get 'views_filters' as the $form_id |
| 93 |
foreach ($view->exposed_filter as $count => $exposed) { |
// In any case, the foreach loop below will do nothing if there are no filters. |
| 94 |
//Start looping through through all exposed filters |
// $view = $form['view']['#value']; |
| 95 |
// ai: Set some references for use below (just for readability and maybe a little performance) |
// if (!isset($view->exposed_filter) ) return; // No exposed filters so nothing to do. |
| 96 |
$this_filter = & $form['filter'.$count]; |
// if (!count($view->exposed_filter) ) return; // No exposed filters so nothing to do. |
| 97 |
$this_type = & $this_filter['#type']; $these_options = & $this_filter['#options']; |
|
| 98 |
|
// Get configuration values... |
| 99 |
// This is probably not necessary currently, but just in case Views module changes: |
$limit = variable_get('views_checkboxes_limit', 5); |
| 100 |
if ($this_type != 'select') continue; // Nothing useful to do so quick exit |
$list_size = variable_get('views_checkboxes_listsize', FALSE); |
| 101 |
|
$sizeup = variable_get('views_checkboxes_sizeup', FALSE); |
| 102 |
// Remove the "None selected" option which (sometimes/always?) appears for Taxonomy terms: |
$checkbox_enable = variable_get('views_checkboxes_checkbox_enable', FALSE); |
| 103 |
if (isset($these_options['']) && variable_get('views_checkboxes_remove_none', FALSE)) |
$radio_enable = variable_get('views_checkboxes_radio_enable', FALSE); |
| 104 |
unset($these_options['']); |
$remove_none = variable_get('views_checkboxes_remove_none', FALSE); |
| 105 |
|
|
| 106 |
$list_count = count($these_options); // Get the number of options that will be listed |
// Now loop through through all exposed filters... |
| 107 |
|
foreach ($form['view']['#value']->exposed_filter as $count => $exposed) { |
| 108 |
// If enabled, use listsize setting: |
// ai: Set some references for use below (just for readability and maybe a little performance) |
| 109 |
if ($list_size = variable_get('views_checkboxes_listsize', FALSE)) { |
$this_filter = & $form['filter'.$count]; |
| 110 |
// increase listsize by one if enabled and appropriate |
$this_type = & $this_filter['#type']; $these_options = & $this_filter['#options']; |
| 111 |
if (variable_get('views_checkboxes_sizeup', FALSE) && $list_size+1 == $list_count) ++$list_size; |
|
| 112 |
$this_filter['#size'] = $list_size; |
// This is probably not necessary currently, but just in case Views module changes: |
| 113 |
} |
if ($this_type != 'select') continue; // Nothing useful to do so quick exit |
| 114 |
|
|
| 115 |
|
// Remove the "None selected" option which appears for "not required" Taxonomy terms: |
| 116 |
|
if (isset($these_options['']) && $remove_none) unset($these_options['']); |
| 117 |
|
|
| 118 |
|
$list_count = count($these_options); // Get the number of options that will be listed |
| 119 |
|
|
| 120 |
|
// If enabled, use listsize setting: |
| 121 |
|
if ($list_size) { |
| 122 |
|
// increase listsize by one if enabled and appropriate |
| 123 |
|
if ($sizeup && $list_size+1 == $list_count) ++$list_size; |
| 124 |
|
$this_filter['#size'] = $list_size; |
| 125 |
|
} |
| 126 |
|
|
| 127 |
// If the number of options that would be displayed is greater than limit, use a listbox anyway. |
// If the number of options that would be displayed is greater than limit, use a listbox anyway. |
| 128 |
if (isset($these_options['**ALL**'])) $list_count--; // Don't count "ALL" as it gets removed below |
if (isset($these_options['**ALL**'])) $list_count--; // Don't count "ALL" as it gets removed below |
| 129 |
if ($list_count > $limit) continue; // Too many options, so use listbox |
if ($list_count > $limit) continue; // Too many options, so use listbox |
| 130 |
|
|
| 131 |
// Check to see if we will have anything more to do and set up new types if so |
// Check to see if we will have anything more to do and set up new types if so |
| 132 |
switch ($exposed['single']) { |
switch ($exposed['single']) { |
| 133 |
case 0: // Not single: use checkboxes |
case 0: // Not single: use checkboxes |
| 134 |
if (!variable_get('views_checkboxes_checkbox_enable', false)) continue; // Not enabled, skip |
if (!$checkbox_enable) continue; // Not enabled, skip |
| 135 |
$this_type = 'checkboxes'; // Set new type |
$this_type = 'checkboxes'; // Set new type |
| 136 |
break; |
break; |
| 137 |
case 1: // Single: use radios |
case 1: // Single: use radios |
| 138 |
if (!variable_get('views_checkboxes_radio_enable', false)) continue; // Not enabled, skip |
if (!$radio_enable) continue; // Not enabled, skip |
| 139 |
$this_type = 'radios'; // Set new type |
$this_type = 'radios'; // Set new type |
| 140 |
break; |
break; |
| 141 |
default: |
default: |
| 142 |
continue; // Neither (!?) so skip it anyway |
continue; // Neither (!?) so skip it anyway |
| 143 |
} |
} |
| 144 |
|
|
| 145 |
// If we get this far, we are replacing the listbox with checkboxes/radios |
// If we get this far, we are replacing the listbox with checkboxes/radios |
| 146 |
// Need to unset the theme or else the views module will still make this a select box. |
// Need to unset the theme or else the views module will still make this a select box. |
| 147 |
unset($this_filter['#theme']); |
unset($this_filter['#theme']); |
| 148 |
// Remove the "**ALL**" option if it exists: |
// Remove the "**ALL**" option if it exists: |
| 149 |
if (isset($these_options['**ALL**'])) unset($these_options['**ALL**']); |
if (isset($these_options['**ALL**'])) unset($these_options['**ALL**']); |
| 150 |
|
|
| 151 |
// Taxonomy options will be an array of objects handled here: |
// Taxonomy options will be an array of objects handled here: |
| 152 |
// To do: Make sure everything works for multi-language and multi-level taxonomy lists too |
// To do: Make sure everything works for multi-language and multi-level taxonomy lists too |
| 153 |
if (is_object($these_options[0])) { |
if (is_object($these_options[0])) { |
| 154 |
// This will recreate them as a typical form option array... |
// This will recreate them as a typical form option array... |
| 155 |
$newoptions = array(); |
$newoptions = array(); |
| 156 |
foreach ($these_options as $option_id => $option_obj) { |
foreach ($these_options as $option_id => $option_obj) { |
| 157 |
// $option_obj example: stdClass Object ( [option] => Array ( [60] => English ) |
// $option_obj example: stdClass Object ( [option] => Array ( [60] => English ) |
| 158 |
foreach ($option_obj->option as $num => $val) { |
foreach ($option_obj->option as $num => $val) { |
| 159 |
$newoptions[$num] = $val; |
$newoptions[$num] = $val; |
|
} |
|
| 160 |
} |
} |
|
// Set new options array back in the form. |
|
|
$these_options = $newoptions; |
|
| 161 |
} |
} |
| 162 |
|
// Set new options array back in the form. |
| 163 |
|
$these_options = $newoptions; |
| 164 |
} |
} |
| 165 |
} |
} |
| 166 |
} |
} |