This is a really nice widget, thank you for your on this project.
I would like to assign the widget to be used with certain vocabularies below is the code I came up with that will add the functionality.
Quick review of changes/additions:
- 2 IF statements in the form_alter function
- a. to add the class, css and js to selected vocabularies on node forms
- b. to alter the vocabulary form to add an enable widget option
- a form validate function for the enable widget option
- a form submit function for the enable widget option (values stored in drupal variable)
- a simple function to add the js/css as needed to the node form
I have attached a patch file as well as the code below.
/**
* Implementation of hook_form_alter().
*/
function fancy_multiselect_form_alter(&$form, $form_state, $form_id) {
// Provide additional help for the field settings form.
if ($form_id == 'content_field_edit_form' && isset($form['widget'])) {
/// --the code in this IF statement stays the same....
}
elseif (isset($form['type']) && isset($form['#node'])) {
// add the 'fancy-multiselect' class to enabled vocabularies fields
// add the js/css needed for the widget to work
$vids = variable_get('fancy_multiselect_vids', array());
foreach ($form['taxonomy'] as $vid => $data) {
if ($vids[$vid]) {
$form['taxonomy'][$vid]['#attributes']['class'] .= ' fancy-multiselect';
fancy_multiselect_includes();
}
}
}
elseif ($form_id == 'taxonomy_form_vocabulary') {
// add a fieldset with an enable field to the taxonomy form to allow admins to enable/disable the fancy multiselect with a particular vocabulary
$vids = variable_get('fancy_multiselect_vids', array());
$vid = $form['vid']['#value'];
$form['fancy_multiselect'] = array(
'#tree' => TRUE,
'#type' => 'fieldset',
'#title' => t('Fancy Fieldset'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['fancy_multiselect']['enabled'] = array(
'#type' => 'radios',
'#title' => t('Enable the Fancy Multiselect input widget with this Vocabulary.'),
'#options' => array(0=>'No', 1=>'Yes'),
'#default_value' => $vids[$vid] == 1 ? 1 : 0,
'#description' => t('This widget can only be used with multiselect vocabularies.'),
);
// add validation callback
$form['#validate'][] = 'fancy_multiselect_taxonomy_form_validate';
// add submit callback
$form['#submit'][] = 'fancy_multiselect_taxonomy_form_submit';
}
}
/**
* Implementation of hook_FORM_validate().
* Validates the enable fancy multiselect field on the that was added to the 'taxonomy_form_vocabulary' form via hook_form_alter
* Returns an error if user attempts to enable fancy multiselect with a non multiselect vocabulary
*/
function fancy_multiselect_taxonomy_form_validate($form, &$form_state) {
if ($form_state['values']['multiple'] == 0 && $form_state['values']['fancy_multiselect']['enabled'] == 1) {
form_set_error('fancy_multiselect][enabled', t('Fancy Multiselect input widget can only be used with multiselect vocabularies'));
}
}
/**
* Implementation of hook_FORM_submit().
* Process the enable fancy multiselect field on the that was added to the 'taxonomy_form_vocabulary' form via hook_form_alter
* Saves the enabled value (0 or 1) in an array keyed by the vid of the vocabulary in drupal variable named 'fancy_multiselect_vids'
*/
function fancy_multiselect_taxonomy_form_submit($form, &$form_state) {
$vids = variable_get('fancy_multiselect_vids', array());
$vid = $form_state['values']['vid'];
$enabled = $form_state['values']['fancy_multiselect']['enabled'];
$vids[$vid] = $enabled;
variable_set('fancy_multiselect_vids', $vids);
}
/*
* simple function to add necessary js and css
*/
function fancy_multiselect_includes(){
static $added;
if (!$added) {
// only call this the first time....
///----------------------------------------------------------------------------------------///
/// THIS CODE IS COPIED FROM fancy_multiselect_select_process BELOW
/// Ideally that function (fancy_multiselect_select_process) would be changed
/// to call this function to add the css and js
///----------------------------------------------------------------------------------------///
// Insert Javascript and CSS for this widget.
$path = drupal_get_path('module', 'fancy_multiselect');
$jquery_path = drupal_get_path('module', 'jquery_ui');
drupal_add_css($path .'/css/ui.multiselect.css');
drupal_add_css($jquery_path.'/jquery.ui/themes/base/ui.all.css');
if (module_exists('jq')) {
jq_add('multiSelect');
}
else {
jquery_multiselect_plugin_add();
}
//TODO make configurable by ckk widget
drupal_add_js('$(function(){$(".fancy-multiselect").multiselect({
sortable: true,
searchable: true,
animated: "fast",
show: "slideDown",
hide: "slideUp",
dividerLocation: 0.5
});});','inline','header'
);
///----------------------------------------------------------------------------------------///
/// END OF CODE COPIED FROM fancy_multiselect_select_process BELOW
///----------------------------------------------------------------------------------------///
$added = TRUE;
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| fancy_multiselect.module.patch | 5.34 KB | rjl |
Comments
Comment #1
rjl commentedAppologies for using the CODE tag above and not PHP tag (and can't edit above)