By popular demand, I've modified the taxonomy checkboxes code to do indentation based upon taxonomy depth. I dunno if it's perfect but I think it works reasonably well.

Here's a screenshot (a crappy one, needs some CSS but that shouldn't be too hard.)


And here's the code:

<?php
function test_expand_checkboxes($element) {
 
$value = is_array($element['#value']) ? $element['#value'] : array();
 
$element['#tree'] = TRUE;
  if (
count($element['#options']) > 0) {
    if (!isset(
$element['#default_value']) || $element['#default_value'] == 0) {
     
$element['#default_value'] = array();
    }
    foreach (
$element['#options'] as $key => $choice) {
      if (!isset(
$element[$key])) {
       
$tchoice = ltrim($choice, '-');
       
$depth = strlen($choice) - strlen($tchoice);
       
$element[$key] = array(
         
'#type' => 'checkbox',
         
'#prefix' => '<div style="margin-left: ' . (10 * $depth) . 'px">',
         
'#suffix' => '</div>',
         
'#processed' => TRUE,
         
'#title' => $tchoice,
         
'#return_value' => $key,
         
'#default_value' => isset($value[$key]),
         
'#attributes' => $element['#attributes']);
      }
    }
  }
  return
$element;
}

function
test_expand_radios($element) {
  if (
count($element['#options']) > 0) {
    foreach (
$element['#options'] as $key => $choice) {
      if (!isset(
$element[$key])) {
       
$tchoice = ltrim($choice, '-');
       
$depth = strlen($choice) - strlen($tchoice);
       
$element[$key] = array(
         
'#type' => 'radio',
         
'#prefix' => '<div style="margin-left: ' . (10 * $depth) . 'px">',
         
'#suffix' => '</div>',
         
'#title' => $tchoice,
         
'#return_value' => $key,
         
'#default_value' => $element['#default_value'],
         
'#attributes' => $element['#attributes'],
         
'#parents' => $element['#parents'],
         
'#spawned' => TRUE);
      }
    }
  }
  return
$element;
}

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';
           
$form['taxonomy'][$vid]['#process'] = array('test_expand_checkboxes' => array());
          }
          else {
           
$form['taxonomy'][$vid]['#type'] = 'radios';
           
$form['taxonomy'][$vid]['#default_value'] = $form['taxonomy'][$vid]['#default_value'][0];
           
$form['taxonomy'][$vid]['#process'] = array('test_expand_radios' => array());
          }

          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>';
        }
      }
    }
  }
}
?>

And here's some improved CSS

.taxonomy-form {
  float: left;
  margin-right: 3em;
}
.taxonomy-form .form-item {
  margin-bottom: 2px;
  margin-top: 0;
  padding-bottom: 0;
}

AttachmentSize
taxocheck.png6.84 KB
taxocheck2.png6.3 KB

hehe awesome :)

add bit of collapseability and w3333 :D
nice work mate.

When Drupal has a

When Drupal has a collapsible div that isn't a fieldset, it'll be trivial.

This would be great for assigning blocks!

I wish there was an easy way to make something like this work for assign blocks.

All I have right now is php code that must be edited each time I add a new taxonomy term. I would love to be able to just add another click.

why DIVS with hardcoded CSS?

... why not just ULs and LIs? IT will:
a) make nesting a lot lot lot lot easier.
b) not require hardcoded <div style="margin-left: ' . (10 * $depth) . 'px"> which is worse then a sin :) (esp because its not transparantly themeable)
c) make the code semantically more correct
d) downgrade to all browsers, not depend on CSS and work without stylesheets too (ie mobile devices, screen readers etc).

But all in all, the concept is very very leet. IT allows one to add a small overflow:ato in his/her theme to handle large taxonomies too. great work!

Bèr

a) The nesting isn't

a) The nesting isn't entirely easy with the formapi. I thought about it a bit and realized that I could do it but I didn't want to spend the time fiddling with it to get it right.
b) This is already in the theming layer.

Honestly, this is a quick hack, not Real Published Code, that I'm sharing.

What's funny is that i just posted a patch to the threaded comments in Drupal to use nested divs rather than the hardcoded stuff we use...then I went and did the hardcoding for it here =)

How to install it??

Hi, I followed discussions on this node : http://drupal.org/node/84286 and I ended up here trying this code, but I just can't install it, it does not work at all. I installed a module (i named it alter_form_select.module and enabled the moudle and nothing happened. POsible to have a little tip on how to make it work cause it look really great and useful!

Thanks,
patchak
cool site btw

Howto

The word 'test' in all of the function names is the name of the module.

If you put this in your own module, replace 'test' with the name of the module you used; then all should just work.

Thanks I followed your

Thanks I followed your instructions and made it work. I added the css to style.css as well but the checkboxes are not displayed like in your example, does that codegoes in that file or in another css file?

Thanks

Little bug....

Hi, I'm using this on a site and it seems that when I add a single select 'required" vocab to a node type there is no option selected by default.

It's a problem since mots people don,t see it and don't select it and then they receive a messgae such like "an illegal procesure has been detected, etc".

Do you know how to make sure that there is a 'selected default' for the radio buttons?

Thanks

doesn't seem to work for me

I tried this with the recipe module and page module and it doesn't seem to be working. I put a print r $form command in there and it looks like there is no reference to the taxonomy in the resulting dump. Is form_alter somehow getting called before the taxonomy's form_alter has been added? Running latest full release of 4.7. Any suggestions would be appreciated.

Figured it out

I did a bit more reading in the forum post on drupal.org and figured out that I had to change the weight of the module in the system table. It's working fine now. Sorry for the post and thanks for the module.

How to apply this to exposed filters

Hey,
I was trying to apply this not only to the creation page but also to the exposed filters within a taxonomy view. Can anyone give me a hint on this?
Cheers

Got it showing up, but...

Got it showing up by adding a .info file, but how do you make it do something?

Guess I'm just a dumb newbie

I cut and pasted the code into a test.module in my sites/mysite folder. But it does not show up when I go to Administer>>Site building>>Modules to enable it.

One that problem is solved, how do I invoke this code?

Thanks,
Nancy

Oh, one more silly request

Can you add the number of stories in each term?

how to add check boxes to the contact form's category?

i would like to know whether there are any solution or way to add checkboxes in the contact form(at category section)? i also want to add some field into that form.. sorry for my English.. i really need some help here.. Thanks in advance.

Drupal 6?

Anyone get a version of this working in drupal 6?

Not working in 5.7

In D5.7 every term label has the name "Object" instead of the actual term name. Please help as this is the last elegant checkbox solution of D5.

Not Working as of 5.x-3.1?

Actually as far as i can tell it was not working as of 5.x-3.1 - does anyone have it working on 5.x-3.1 or later?

Seems to be related to the same problem someone was having here: http://drupal.org/node/231289

Drupal 6.4 Port

Hi

In comment:

http://drupal.org/node/84286#comment-1041701

I posted an updated version that supports Drupal 6.4.
Besides the normal node editing taxonomy it also handles tax_lite
taxonomy fields.

Btw is it a good idea to turn this into a 'real' drupal downloable module?

Post new comment

The content of this field is kept private and will not be shown publicly.