| 1 |
<?php
|
| 2 |
|
| 3 |
function views_checkboxes_help($section='') {
|
| 4 |
$output = '';
|
| 5 |
$newFeatures = '<p>Also, you can optionally override the size of select lists (the number of elements displayed).</p>';
|
| 6 |
|
| 7 |
switch ($section) {
|
| 8 |
case 'admin/help#views_checkboxes':
|
| 9 |
$output .= '<p>Alters view filter forms to replace select elements wth checkboxes and radio buttons.</p>';
|
| 10 |
$output .= $newFeatures;
|
| 11 |
break;
|
| 12 |
case 'admin/settings/views_checkboxes':
|
| 13 |
$output .= '<p>Enable this module to replace all select form elements in view filters with checkboxes or radio buttons, as appropriate.</p>';
|
| 14 |
$output .= $newFeatures;
|
| 15 |
break;
|
| 16 |
}
|
| 17 |
return $output;
|
| 18 |
}
|
| 19 |
|
| 20 |
function views_checkboxes_menu() {
|
| 21 |
$items = array();
|
| 22 |
|
| 23 |
$items[] = array(
|
| 24 |
'path' => 'admin/settings/views_checkboxes',
|
| 25 |
'title' => t('Views checkboxes'),
|
| 26 |
'description' => t('Alter view filter forms'),
|
| 27 |
'callback' => 'drupal_get_form',
|
| 28 |
'callback arguments' => 'views_checkboxes_admin',
|
| 29 |
'access' => user_access('administer views'),
|
| 30 |
'type' => MENU_NORMAL_ITEM
|
| 31 |
);
|
| 32 |
|
| 33 |
return $items;
|
| 34 |
}
|
| 35 |
|
| 36 |
function views_checkboxes_admin() {
|
| 37 |
$form['views_checkboxes_checkbox_enable'] = array(
|
| 38 |
'#type' => 'checkbox',
|
| 39 |
'#title' => t('Replace multi-select boxes with checkboxes.'),
|
| 40 |
'#default_value' => variable_get('views_checkboxes_checkbox_enable', FALSE),
|
| 41 |
'#description' => t('Enable this to replace all multi-select view filter form elements with checkboxes.'),
|
| 42 |
);
|
| 43 |
|
| 44 |
$form['views_checkboxes_radio_enable'] = array(
|
| 45 |
'#type' => 'checkbox',
|
| 46 |
'#title' => t('Replace single-select boxes with radio buttons.'),
|
| 47 |
'#default_value' => variable_get('views_checkboxes_radio_enable', FALSE),
|
| 48 |
'#description' => t('Enable this to replace all single-select view filter form elements with radio buttons.'),
|
| 49 |
);
|
| 50 |
|
| 51 |
$form['views_checkboxes_limit'] = array(
|
| 52 |
'#type' => 'textfield',
|
| 53 |
'#title' => t('Limit'),
|
| 54 |
'#default_value' => variable_get('views_checkboxes_limit', 5),
|
| 55 |
'#size' => 2,
|
| 56 |
'#description' => t('Do not use checkboxes/radios if the number of options is greater than this value.'),
|
| 57 |
);
|
| 58 |
|
| 59 |
$form['views_checkboxes_listsize'] = array(
|
| 60 |
'#type' => 'textfield',
|
| 61 |
'#title' => t('Size of list'),
|
| 62 |
'#default_value' => variable_get('views_checkboxes_listsize', 5),
|
| 63 |
'#size' => 2,
|
| 64 |
'#description' => t('When using a select-list, display only this many entries. Enter zero to use Views default.'),
|
| 65 |
);
|
| 66 |
|
| 67 |
$form['views_checkboxes_sizeup'] = array(
|
| 68 |
'#type' => 'checkbox',
|
| 69 |
'#title' => t('Increase size of list to fit'),
|
| 70 |
'#default_value' => variable_get('views_checkboxes_sizeup', FALSE),
|
| 71 |
'#description' => t('When the above option is enabled, automatically increase the size by one if that will show the whole list.'),
|
| 72 |
);
|
| 73 |
|
| 74 |
return system_settings_form($form);
|
| 75 |
}
|
| 76 |
|
| 77 |
function views_checkboxes_form_alter($form_id, &$form) {
|
| 78 |
|
| 79 |
if (isset($form['view']['#value']->exposed_filter)) {
|
| 80 |
// view filter forms don't seem to have a form_id, so I'm using this conditional to identify them.
|
| 81 |
|
| 82 |
$view = $form['view']['#value'];
|
| 83 |
$limit = variable_get('views_checkboxes_limit', 5);
|
| 84 |
|
| 85 |
foreach ($view->exposed_filter as $count => $exposed) {
|
| 86 |
//Start looping through through all exposed filters
|
| 87 |
// ai: Set some references for use below (just for readability and maybe a little performance)
|
| 88 |
$thisFilter = & $form['filter'.$count];
|
| 89 |
$thisFilterType = & $thisFilter['#type']; $theseOptions = & $thisFilter['#options'];
|
| 90 |
|
| 91 |
// This is probably not necessary currently, but just in case Views module changes:
|
| 92 |
if ($thisFilterType != 'select') continue; // ai: Nothing useful to do so quick exit
|
| 93 |
|
| 94 |
// Remove the pointless (?) "- None selected -" choice which (maybe?) appears for Taxonomy terms:
|
| 95 |
// (should this be a configurable option?)
|
| 96 |
if (isset($theseOptions[''])) unset($theseOptions['']);
|
| 97 |
|
| 98 |
$listCount = count($theseOptions); // Get the number of options that will be listed
|
| 99 |
|
| 100 |
// If enabled, use listsize setting:
|
| 101 |
if ($listSize = variable_get('views_checkboxes_listsize', FALSE)) {
|
| 102 |
// increase listsize by one if enabled and appropriate
|
| 103 |
if (variable_get('views_checkboxes_sizeup', FALSE) && $listSize+1 == $listCount) ++$listSize;
|
| 104 |
$thisFilter['#size'] = $listSize;
|
| 105 |
}
|
| 106 |
|
| 107 |
// If the number of options that would be displayed is greater than limit, use a listbox anyway.
|
| 108 |
if ($listCount > $limit) continue; // This counts **ALL** too, if present, but it doesn't really matter much.
|
| 109 |
|
| 110 |
// Activate radios/checkbox replacement if appropriate and enabled...
|
| 111 |
if ($exposed['single'] == '1' && variable_get('views_checkboxes_radio_enable', false)) {
|
| 112 |
// This is a select box with the single limitation. Should be radio buttons.
|
| 113 |
$thisFilterType = 'radios';
|
| 114 |
$active = TRUE;
|
| 115 |
}
|
| 116 |
elseif ($exposed['single'] == '0' && variable_get('views_checkboxes_checkbox_enable', false)) {
|
| 117 |
// This is a select box with no single limitation. Should be checkboxes.
|
| 118 |
$thisFilterType = 'checkboxes';
|
| 119 |
$active = TRUE;
|
| 120 |
}
|
| 121 |
else
|
| 122 |
$active = FALSE;
|
| 123 |
|
| 124 |
if ($active) {
|
| 125 |
// We are going to change the ListBox to make checkboxes or radio buttons...
|
| 126 |
// Need to unset the theme or else the views module will still make this a select box.
|
| 127 |
unset($thisFilter['#theme']);
|
| 128 |
// Remove the "**ALL**" option if it exists:
|
| 129 |
if (isset($theseOptions['**ALL**'])) unset($theseOptions['**ALL**']);
|
| 130 |
|
| 131 |
// Taxonomy options will be an array of objects handled here:
|
| 132 |
if (is_object($theseOptions[0])) {
|
| 133 |
// This will recreate them as a typical form option array...
|
| 134 |
$newoptions = array();
|
| 135 |
|
| 136 |
foreach ($theseOptions as $option_id => $option) {
|
| 137 |
/*** ai: "**ALL** is now handled above
|
| 138 |
// I'm disabling the **ALL** option entirely, if it exists.
|
| 139 |
if ($option_id === '**ALL**') continue;
|
| 140 |
****/
|
| 141 |
|
| 142 |
// I'm disabling the "- Please choose -" option some have reported.
|
| 143 |
// ai: What's does this next line actually do? I think it was for "- None selected -", now handled above, so removed here.
|
| 144 |
// ai: disabled: if ($option_id === '') continue;
|
| 145 |
foreach ($theseOptions[$option_id]->option as $num => $val) {
|
| 146 |
$newoptions[$num] = $val;
|
| 147 |
}
|
| 148 |
}
|
| 149 |
$theseOptions = $newoptions; // Copy new options back to the form.
|
| 150 |
}
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|
| 154 |
}
|