Posted by Merlin on Thu Sep 14, 2006 8:28 pm.
-- style.css --
Someone asked for a module to turn taxonomy into checkboxes. I'd seen it done somewhere before but couldn't find the original, but the concept seemed easy enough so I whipped up one of my own.
[Updated about 30m after original publishing with a less broken version, also a nice CSS enhancement to make the checkboxes come up side by side]
<?php
function test_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
if (is_array($form['taxonomy'])) {
foreach ($form['taxonomy'] as $vid => $taxonomy) {
if (is_numeric($vid)) {
if ($taxonomy['#multiple']) {
$form['taxonomy'][$vid]['#type'] = 'checkboxes';
}
else {
$form['taxonomy'][$vid]['#type'] = 'radios';
$form['taxonomy'][$vid]['#default_value'] = $form['taxonomy'][$vid]['#default_value'][0];
}
unset($form['taxonomy'][$vid]['#theme']);
if ($form['taxonomy'][$vid]['#options'][0]) {
unset($form['taxonomy'][$vid]['#options'][0]);
}
else {
$form['taxonomy'][$vid]['#required'] = TRUE;
}
$form['taxonomy'][$vid]['#prefix'] = '<div class="taxonomy-form">';
$form['taxonomy'][$vid]['#suffix'] = '</div>';
}
}
}
}
}
?>-- style.css --
.taxonomy-form {
float: left;
margin-right: 3em;
}

You lost me on this one
Forgive me for being a little dense, but how is this used? To replace the dropdown list normally used for for selecting a taxonomy term?
If that's right it sounds like a great idea for simple sites. I'm working on another, though, that will likely have a couple dozen terms in one list and several more in a couple other lists.
I wish there was a better way to manage larger numbers of terms when neither dropdowns or checkboxes are practical?
Yes, that's exactly what
Yes, that's exactly what it's for. Sometimes checkboxes are closer to what you want.
It could be further enhanced, likely, by using more forms api theming so that the checkboxes show up in columns. With relatively short terms, one could probably fit 5 across, so 30 would only take 6 lines. That's not too bad at all.
Got it.
Sorry for being slow. Checkboxes in columns makes a lot of sense. It just took me a while to think it through.
I'm wondering, too, if in situations where so many terms make checkboxes and scroll down lists cumbersome that some sort of Ajax menu tree would work. Unfortunately, that's well beyond my skills to create.
jjeff was just telling me,
jjeff was just telling me, last night, that he'd taken this code and gonee even further -- making multi-select lists basically be checkbox lists inside a scrolling div. Which seems like a really great idea to me.
Nice!
I've been thinking that a scrollable div with a list of checkboxes is a considerably better interface than the multiple select nasty user interface with some sort of explanation telling users to hold down the control/command key to select multiple items. Aweful!
I'd love to expand this out to replace ALL multiple-select fields in Drupal. And to make it even a bit sexier, I'd like to add a little bit of JavaScript to highlight the row when it is selected.
Word!
Merlin, this is great stuff. Enjoy the Mcluhan book.
This code didn't work for me, but was a good start.
I am using Drupal 5.6 and when i tried this code i got errors about invalid stdObjects. The reason I think is that my taxonomy #options array looks like this:
[#options] => Array ( [0] => [1] => stdClass Object ( [option] => Array ( [7] => Term A ) ) [2] => stdClass Object ( [option] => Array ( [8] => Term B ) ) )and for checkboxes and radios, the options are expected to be:
[#options] => Array ( [7] => Term A [8] => Term B )So, simply setting the existing options as the new options isn't enough, I think. Not sure if there's a better way to do this, but the code I came up with is:
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
if (is_array($form['taxonomy'])) {
foreach ($form['taxonomy'] as $vid => $taxonomy) {
if (is_numeric($vid)) {
if ($taxonomy['#multiple']) {
$form['taxonomy'][$vid]['#type'] = 'checkboxes';
}
else {
$form['taxonomy'][$vid]['#type'] = 'radios';
$form['taxonomy'][$vid]['#default_value'] = $form['taxonomy'][$vid]['#default_value'][0];
}
unset($form['taxonomy'][$vid]['#theme']);
if ($form['taxonomy'][$vid]['#options'][0]) {
//not needed now//unset($form['taxonomy'][$vid]['#options'][0]);
}
else {
$form['taxonomy'][$vid]['#required'] = TRUE;
}
$newOptions = array();
foreach($form['taxonomy'][$vid]['#options'] as $taxObj) {
if ($taxObj && $taxObj->option && is_array($taxObj->option)) {
foreach($taxObj->option as $id => $name) {
$newOptions[$id] = $name;
}
}
}
$form['taxonomy'][$vid]['#options'] = $newOptions;
$form['taxonomy'][$vid]['#prefix'] = '<div class="taxonomy-form">';
$form['taxonomy'][$vid]['#suffix'] = '</div>';
}
}
}
}
This works well. Let me know your thoughts... maybe I am not thinking right.
Thanks!
I was in the same boat as Matthew Sielski above - and his solution worked for just perfectly. Thanks for the collaboration that made my day easier.
It's a module now!
webchick created a D6 module to do this: http://drupal.org/project/betterselect
Post new comment