hello,

I would like to know if it's possible to create a "hierarchical" checkboxes list in a form. I have a hierarchical taxonomy and would like to display it like this :

item 1 (enabled)
item 2 (disabled)
- item 2 sub-item 1 (enabled)
- item 2 sub-item 2 (enabled)
- item 2 sub-item 3 (enabled)
item 3 (enabled)
item 4 (enabled)
item 5 (disabled)
- item 5 sub-item 1 (enabled)
- item 5 sub-item 2 (enabled)
...

I wouldn't display it like a simple list like :

item 1
item 2
item 2 sub-item 1
item 2 sub-item 2
item 2 sub-item 3
item 3
item 4
...

but really in a hiearchical way, I mean that sub-items must be indented. And of course without using a specific module, just in a programmatic manner.

Thanks in advance

Comments

Raf’s picture

Seeing as checkboxes are handled as arrays, I suppose you could use multi-dimensional arrays for the nesting. Never tried it myself, but I don't see why it wouldn't work.

For the form, you could add a class to each element, making it indented using CSS. Can't remember off the top of my head whether it's in #prefix or if there's an actual parameter you can set for classes.

titouille’s picture

Hi Raf,

I finally found a way to do it.

I always use this syntax :

$form['myboxes'] = array(
  '#type' => 'checkboxes',
  '#weight' => 10,
  '#default_values' => array( ... ),
  '#options' => array( ... my options ... ),
);

and didn't know that I can use this syntax :


$form['myboxes'] = array( 
  '#type' => 'checkboxes',
  0 => array( '#type' => 'checkbox', ... ),
  1 => array( '#type' => 'checkbox', ... ),
);

With this, I can put all my checkboxes in an array and simply add correct string keys (like '#type' = 'checkboxes', '#title' = 'my taxonomy name', '#weight' = 10, etc...) into to have a field with multiple checkboxes with each checkbox like I want (parent must be disabled and italic name, with #prefix / #suffix to apply indentation with css like you sugger).

Thanks for your answer and tip about css ;-)

Raf’s picture

...

Waaaaay better solution. I just realized multi-dimensional arrays won't work with the form API =x

reaper2006’s picture

Hi,

In really i use your code, but this code only present values in list, and don't represent and hierarchical values.
Í use this:

$form['novo'] = array(
 '#type' => 'checkboxes',
   '#title' => t('Default options'),
  '#default_value' => variable_get('node_options_'. $node->type, array('status', 'promote')),
  '#options' => array(
    'status' => t(' Published'),
    'moderate' => t('In moderation queue'),
    'promote' => t('Promoted to front page'),
    'sticky' => t('Sticky at top of lists'),
    'revision' => t('Create new revision'),
  ),
  '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'),


);

And the checkboxes appear as list.

Can you help me?

Thanks

titouille’s picture

Hello reaper2006.

You can do this :

$form['novo'] = array(
'#type' => 'checkboxes',
   '#title' => t('Default options'),
  '#default_value' => variable_get('node_options_'. $node->type, array('status', 'promote')),
  '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'
  0 => array( 
      '#type' => 'checkbox',
      '#prefix' => "<div class='myclass-status'>",
      '#suffix' => '</div>',
      '#title' => t('Published'),
      '#return_value' => 'status',
      '#disabled' => FALSE,
      '#default_value' => 0,
   ),
  1 => array( 
      '#type' => 'checkbox',
      '#prefix' => "<div class='myclass-moderate'>",
      '#suffix' => '</div>',
      '#title' => t('In moderation queue'),
      '#return_value' => 'moderate',
      '#disabled' => FALSE,
      '#default_value' => 0,
   ),
   2 => array( ... ),
   ...
);

Like you see, you can declare each checkbox as a form item with numerical index beginning by 0.

By using #prefix and #suffix attributes, you can embed your checkbox in any container (div, span, etc...) and can use css to make your own design.

Hope this help

reaper2006’s picture

Hi titouille ,
Many thanks.

In really this solution help me in another problem, however, i need another solution, if exist to my problem. I pass to explain.

I have an array like this :

   $tree_data = array();


       $tree_data[0] = array(

       url => "",

       parent => "0",

       name => "test");
  
 tree_data [1] =array(
url => "",
parent = "test",
name => "test1",
); 

In really i need to put this in a drupal form with checkboxes, that enable when i press submit button get checkboxes that were checked.....

Have any idea how i do this....

Many Thanks

titouille’s picture

Hi reaper2006,

You can iterate on your array to get your data as hierarchical array like :


$a = array(
  array( 
    "name" => "test",
    "url" => "",
    "parent" => 0,
    "children" => array(
      "name" => "test1",
      "url" => "",
      "parent" => "test",
      "children" => array( ... ),
    ),
  array( ... ),
  ...
);

and reiterate on your result array (maybe with a recursive method) to get each element and use the #prefix / #suffix to create your new form item with checkboxes, exactly like I explained it in my previous message.

veryrose’s picture

Many thanks, this is a good solution for me

alok1234’s picture

Using webform 3.x, drupal 6.28

I am stuck at this stage, where parent and child terms both are displayed as check boxes one after another.

I want a tree structure where parent terms are disabled, users make a selection of say 10 choices, and the webform saves the whole hierarchy, so that non IT user sitting at backend can just download submissions and know parent and child term. Its for publishing.

Any help would be appreciated.

Thanks,
Alok

carlovdb’s picture

Is this also possible in Drupal 7? Because I tried this and all I get is a checkbox with the text "Array" behind the box