| 1 |
<?php |
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
function views_checkboxes_help($section='') { |
/** |
| 5 |
|
* Implementation of hook_help(). |
| 6 |
|
*/ |
| 7 |
|
function views_checkboxes_help($path, $arg) { |
| 8 |
$output = ''; |
$output = ''; |
| 9 |
$newFeatures = '<p>Also, you can optionally override the size of select lists (the number of elements displayed).</p>'; |
$new_features = '<p>Also, you can override the default size of select lists (the number of elements displayed).</p>'; |
| 10 |
|
|
| 11 |
switch ($section) { |
switch ($path) { |
| 12 |
case 'admin/help#views_checkboxes': |
case 'admin/help#views_checkboxes': |
| 13 |
$output .= '<p>Alters view filter forms to replace select elements wth checkboxes and radio buttons.</p>'; |
$output .= '<p>Alters View filter forms to replace select elements wth checkboxes and radio buttons.</p>'; |
| 14 |
$output .= $newFeatures; |
$output .= $new_features; |
| 15 |
break; |
break; |
| 16 |
case 'admin/settings/views_checkboxes': |
case 'admin/settings/views_checkboxes': |
| 17 |
$output .= '<p>Enable this module to replace all select form elements in view filters with checkboxes or radio buttons, as appropriate.</p>'; |
$output .= '<p>Enable this module to replace all select form elements in View filters with checkboxes or radio buttons, as appropriate.</p>'; |
| 18 |
$output .= $newFeatures; |
$output .= $new_features; |
| 19 |
break; |
break; |
| 20 |
} |
} |
| 21 |
return $output; |
return $output; |
| 22 |
} |
} |
| 23 |
|
|
| 24 |
|
/** |
| 25 |
|
* Implementation of hook_menu(). |
| 26 |
|
*/ |
| 27 |
function views_checkboxes_menu() { |
function views_checkboxes_menu() { |
| 28 |
$items = array(); |
$items['admin/settings/views_checkboxes'] = array( |
| 29 |
|
'title' => 'Views checkboxes', |
| 30 |
$items[] = array( |
'description' => 'Alter view filter forms', |
| 31 |
'path' => 'admin/settings/views_checkboxes', |
'page callback' => 'drupal_get_form', |
| 32 |
'title' => t('Views checkboxes'), |
'page arguments' => array('views_checkboxes_admin'), |
| 33 |
'description' => t('Alter view filter forms'), |
'access arguments' => array('administer views'), |
|
'callback' => 'drupal_get_form', |
|
|
'callback arguments' => 'views_checkboxes_admin', |
|
|
'access' => user_access('administer views'), |
|
| 34 |
'type' => MENU_NORMAL_ITEM |
'type' => MENU_NORMAL_ITEM |
| 35 |
); |
); |
| 36 |
|
|
| 37 |
return $items; |
return $items; |
| 38 |
} |
} |
| 39 |
|
|
| 40 |
|
/** |
| 41 |
|
* Administration settings form. |
| 42 |
|
*/ |
| 43 |
function views_checkboxes_admin() { |
function views_checkboxes_admin() { |
| 44 |
$form['views_checkboxes_checkbox_enable'] = array( |
$form['views_checkboxes_checkbox_enable'] = array( |
| 45 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 73 |
|
|
| 74 |
$form['views_checkboxes_sizeup'] = array( |
$form['views_checkboxes_sizeup'] = array( |
| 75 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 76 |
'#title' => t('Increase size of list to fit'), |
'#title' => t('Increase size to fit'), |
| 77 |
'#default_value' => variable_get('views_checkboxes_sizeup', FALSE), |
'#default_value' => variable_get('views_checkboxes_sizeup', FALSE), |
| 78 |
'#description' => t('When the above option is enabled, automatically increase the size by one if that will show the whole list.'), |
'#description' => t('When "Size of list" is set, automatically increase it by one if that will show the whole list.'), |
| 79 |
|
); |
| 80 |
|
|
| 81 |
|
$form['views_checkboxes_remove_none'] = array( |
| 82 |
|
'#type' => 'checkbox', |
| 83 |
|
'#title' => t('Remove "None selected"'), |
| 84 |
|
'#default_value' => variable_get('views_checkboxes_remove_none', FALSE), |
| 85 |
|
'#description' => t('Remove the "None selected" option which may otherwise appear in Taxonomy lists.'), |
| 86 |
); |
); |
| 87 |
|
|
| 88 |
return system_settings_form($form); |
return system_settings_form($form); |
| 89 |
} |
} |
| 90 |
|
|
| 91 |
function views_checkboxes_form_alter($form_id, &$form) { |
/** |
| 92 |
|
* Implementation of hook_form_alter(). |
| 93 |
|
*/ |
| 94 |
|
function views_checkboxes_form_alter(&$form, $form_state, $form_id) { |
| 95 |
|
// views which have exposed filters get a $form_id of 'views_exposed_form' |
| 96 |
|
// with views_Filterblock module installed, $form_id can be 'views_filterblock' |
| 97 |
|
if ($form_id != 'views_exposed_form' && $form_id != 'views_filterblock') { |
| 98 |
|
return; |
| 99 |
|
} |
| 100 |
|
|
| 101 |
if (isset($form['view']['#value']->exposed_filter)) { |
$limit = variable_get('views_checkboxes_limit', 5); |
|
// view filter forms don't seem to have a form_id, so I'm using this conditional to identify them. |
|
| 102 |
|
|
| 103 |
$view = $form['view']['#value']; |
// now loop through through all form elements of the exposed filter form |
| 104 |
$limit = variable_get('views_checkboxes_limit', 5); |
foreach (element_children($form) as $form_element) { |
| 105 |
|
// only operate on the select lists |
| 106 |
|
if (!is_array($form[$form_element]) || $form[$form_element]['#type'] != 'select') { |
| 107 |
|
continue; |
| 108 |
|
} |
| 109 |
|
|
| 110 |
foreach ($view->exposed_filter as $count => $exposed) { |
// remove the "None selected" taxonomy option |
| 111 |
//Start looping through through all exposed filters |
views_checkboxes_remove_none_selected($form[$form_element]); |
|
// ai: Set some references for use below (just for readability and maybe a little performance) |
|
|
$thisFilter = & $form['filter'.$count]; |
|
|
$thisFilterType = & $thisFilter['#type']; $theseOptions = & $thisFilter['#options']; |
|
|
|
|
|
// This is probably not necessary currently, but just in case Views module changes: |
|
|
if ($thisFilterType != 'select') continue; // ai: Nothing useful to do so quick exit |
|
|
|
|
|
// Remove the pointless (?) "- None selected -" choice which (maybe?) appears for Taxonomy terms: |
|
|
// (should this be a configurable option?) |
|
|
if (isset($theseOptions[''])) unset($theseOptions['']); |
|
|
|
|
|
$listCount = count($theseOptions); // Get the number of options that will be listed |
|
|
|
|
|
// If enabled, use listsize setting: |
|
|
if ($listSize = variable_get('views_checkboxes_listsize', FALSE)) { |
|
|
// increase listsize by one if enabled and appropriate |
|
|
if (variable_get('views_checkboxes_sizeup', FALSE) && $listSize+1 == $listCount) ++$listSize; |
|
|
$thisFilter['#size'] = $listSize; |
|
|
} |
|
| 112 |
|
|
| 113 |
// If the number of options that would be displayed is greater than limit, use a listbox anyway. |
// change the list size if required |
| 114 |
if ($listCount > $limit) continue; // This counts **ALL** too, if present, but it doesn't really matter much. |
$list_count = views_checkboxes_alter_list_size(&$form[$form_element]); |
| 115 |
|
|
| 116 |
// Activate radios/checkbox replacement if appropriate and enabled... |
// check the list size is not larger than the user-defined limit |
| 117 |
if ($exposed['single'] == '1' && variable_get('views_checkboxes_radio_enable', false)) { |
if ($list_count > $limit) { |
| 118 |
// This is a select box with the single limitation. Should be radio buttons. |
continue; |
| 119 |
$thisFilterType = 'radios'; |
} |
|
$active = TRUE; |
|
|
} |
|
|
elseif ($exposed['single'] == '0' && variable_get('views_checkboxes_checkbox_enable', false)) { |
|
|
// This is a select box with no single limitation. Should be checkboxes. |
|
|
$thisFilterType = 'checkboxes'; |
|
|
$active = TRUE; |
|
|
} |
|
|
else |
|
|
$active = FALSE; |
|
| 120 |
|
|
| 121 |
if ($active) { |
// replace the selects with checkboxes/radios |
| 122 |
// We are going to change the ListBox to make checkboxes or radio buttons... |
views_checkboxes_replace_selects($form[$form_element]); |
| 123 |
// Need to unset the theme or else the views module will still make this a select box. |
} |
| 124 |
unset($thisFilter['#theme']); |
} |
| 125 |
// Remove the "**ALL**" option if it exists: |
|
| 126 |
if (isset($theseOptions['**ALL**'])) unset($theseOptions['**ALL**']); |
/** |
| 127 |
|
* Helper function that alters the number of options in the list |
| 128 |
// Taxonomy options will be an array of objects handled here: |
*/ |
| 129 |
if (is_object($theseOptions[0])) { |
function views_checkboxes_alter_list_size(&$form_element_obj) { |
| 130 |
// This will recreate them as a typical form option array... |
$list_size = variable_get('views_checkboxes_listsize', FALSE); |
| 131 |
$newoptions = array(); |
$sizeup = variable_get('views_checkboxes_sizeup', FALSE); |
| 132 |
|
|
| 133 |
foreach ($theseOptions as $option_id => $option) { |
// get the number of options that will be listed |
| 134 |
/*** ai: "**ALL** is now handled above |
$list_count = count($form_element_obj['#options']); |
| 135 |
// I'm disabling the **ALL** option entirely, if it exists. |
|
| 136 |
if ($option_id === '**ALL**') continue; |
// if the user has set a maximum list size, we alter the list |
| 137 |
****/ |
if ($list_size) { |
| 138 |
|
// but first, we see if increasing the list size by 1 will allow us to |
| 139 |
// I'm disabling the "- Please choose -" option some have reported. |
// include all options |
| 140 |
// ai: What's does this next line actually do? I think it was for "- None selected -", now handled above, so removed here. |
if ($sizeup && $list_size + 1 == $list_count) { |
| 141 |
// ai: disabled: if ($option_id === '') continue; |
++$list_size; |
| 142 |
foreach ($theseOptions[$option_id]->option as $num => $val) { |
} |
| 143 |
$newoptions[$num] = $val; |
|
| 144 |
} |
// set the new size |
| 145 |
} |
$form_element_obj['#size'] = $list_size; |
| 146 |
$theseOptions = $newoptions; // Copy new options back to the form. |
} |
| 147 |
} |
|
| 148 |
|
// don't count "ALL" as it gets removed |
| 149 |
|
if (isset($form_element_obj['#options']['**ALL**'])) { |
| 150 |
|
$list_count--; |
| 151 |
|
} |
| 152 |
|
|
| 153 |
|
return $list_count; |
| 154 |
|
} |
| 155 |
|
|
| 156 |
|
/** |
| 157 |
|
* Helper function that removes the "None selected" taxonomy option that |
| 158 |
|
* appears for "not required" taxonomy terms. |
| 159 |
|
*/ |
| 160 |
|
function views_checkboxes_remove_none_selected(&$form_element_obj) { |
| 161 |
|
$remove_none = variable_get('views_checkboxes_remove_none', FALSE); |
| 162 |
|
|
| 163 |
|
if ($remove_none && isset($form_element_obj['#options'][''])) { |
| 164 |
|
unset($form_element_obj['#options']['']); |
| 165 |
|
} |
| 166 |
|
} |
| 167 |
|
|
| 168 |
|
/** |
| 169 |
|
* Helper function that handles the replacing of selects with checkboxes/radios |
| 170 |
|
*/ |
| 171 |
|
function views_checkboxes_replace_selects(&$form_element_obj) { |
| 172 |
|
$checkbox_enable = variable_get('views_checkboxes_checkbox_enable', FALSE); |
| 173 |
|
$radio_enable = variable_get('views_checkboxes_radio_enable', FALSE); |
| 174 |
|
|
| 175 |
|
if (!$checkbox_enable && !$radio_enable) { |
| 176 |
|
return; |
| 177 |
|
} |
| 178 |
|
|
| 179 |
|
// make the changes to the form element #type, depending on settings |
| 180 |
|
if ($checkbox_enable && $form_element_obj['#multiple']) { |
| 181 |
|
$form_element_obj['#type'] = 'checkboxes'; |
| 182 |
|
} |
| 183 |
|
else if ($radio_enable && !$form_element_obj['#multiple']) { |
| 184 |
|
$form_element_obj['#type'] = 'radios'; |
| 185 |
|
} |
| 186 |
|
|
| 187 |
|
// need to unset the theme else the views module will make this a select box. |
| 188 |
|
unset($form_element_obj['#theme']); |
| 189 |
|
|
| 190 |
|
// remove the "**ALL**" option if it exists: |
| 191 |
|
if (isset($form_element_obj['#options']['**ALL**'])) { |
| 192 |
|
unset($form_element_obj['#options']['**ALL**']); |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
// additional processing needed for taxonomy terms - they are an array of objects so need to be converted... |
| 196 |
|
// NB: $form_element_obj['#options'][0] can be '- None -' with 'optional' taxonomy terms, so we test [0] and [1] |
| 197 |
|
if (is_object($form_element_obj['#options'][1]) || is_object($form_element_obj['#options'][0])) { |
| 198 |
|
$newoptions = array(); |
| 199 |
|
|
| 200 |
|
foreach ($form_element_obj['#options'] as $option_id => $option_obj) { |
| 201 |
|
// fix warning under rare circumstances, see http://drupal.org/node/235873 |
| 202 |
|
if (!isset($option_obj->option)) { |
| 203 |
|
continue; |
| 204 |
|
} |
| 205 |
|
foreach ($option_obj->option as $num => $val) { |
| 206 |
|
$newoptions[$num] = $val; |
| 207 |
} |
} |
| 208 |
} |
} |
| 209 |
|
// set new options array back in the form. |
| 210 |
|
$form_element_obj['#options'] = $newoptions; |
| 211 |
} |
} |
| 212 |
} |
} |