Display CCK_Taxonomy field as checkboxes

regx - May 7, 2008 - 08:15
Project:CCK Taxonomy Fields
Version:5.x-1.2
Component:User interface
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

Sorry this is not a patch. I am busy on a project and built this into a project module that produces and handles cck forms. When I am finished I will make this a proper patch to cck_taxonomy. Unless someone beats me to it, it shouldn't be that hard to do as the process is pretty simple. It took me much longer than it should have to come up with this work around. I hope this post can save someone some time.

The first thing I tried was to simply theme the form element, but the module I am working on has a high system weight because it needs to load after other modules so that it runs last. Whenever I themed the element it would render outside of the group. I have noticed this behavior with the cck filefield module as well.

Anyway, here was the simple solution I came up with.

1. Create a module with a form_alter hook
2. Create a function to cck_taxonomy_to_checkboxes
3. create a function to convert from checkboxes_to_cck_taxonomy
4. create a form submit function
5. In the form alter hook pass in any form items you want to display as checkboxes to the convert cck_taxonomy_to_checkboxes function
6. In the form alter hook set $form['#submit'] to pass to your submit function and then the normal submit function
7. In your form submit function pass the checkbox $form_values to your checkboxes_to_cck_taxonomy function - basically all this function does is remove items with a value of 0 (unchecked) from the options array.

Here is some sample code for a module named cck_test which alters the form for a cck noded named test_cck_node and converts a field named cck_taxonomy_test to checkboxes

<?php
//$Id$

/**
* @file: cck_test.module
* CCK Taxonomy to checkbox test
* @author: regx
*/

/**
*  Implementation of hook_form_alter().
*/
function cck_test_form_alter($form_id, &$form){
# This module has to load after other form alter modules for this to work, set the system weight to 10 or something
 
if($form_id == 'test_cck_node_form'){
   
cck_test_cck_taxonomy_to_checkboxes($form['field_cck_taxonomy_test']);
   
$form['#submit'] = array(
                                 
'cck_test_form_submit' => array($form_id,&$form_values),
                                 
'node_form_submit'=> array($form_id,&$form_values),
                              );
  }
}
function
cck_test_form_submit($form_id,$form_values){
  
cck_test_checkboxes_to_cck_taxonomy($form_values['field_cck_taxonomy_test']);
}

function
cck_test_cck_taxonomy_to_checkboxes(&$field){
 
# turns cck_taxonomy $field into checkboxes
 
$options = array();
  foreach(
$field['tid']['#options'] as $key => $val){
    if(
is_array($val->option)){
     
$keys = array_keys($val->option);
     
$key = $keys[0];
     
$val = $val->option[$key];
       
$options[$key]= $val;
    }
  }
 
$field['tid']['#type'] = 'checkboxes';
 
$field['tid']['#options'] = $options;
  unset(
$field['tid']['#theme']);
}

function
cck_test_checkboxes_to_cck_taxonomy(&$field){
 
# the label disapears if the term id doesn't exist so get rid of unchecked options
 
foreach($field['tid'] as $key => $val){
    if(!
$val){
      unset(
$field['tid'][$key]);
    }
  }
}
?>

Hopefully this works, I re-factored this in the textbox so I might have missed something.
For my needs this works great and I hope I can implement this in cck_taxonomy and commit a patch in the near future.

The content_taxonomy module has this option, but the views code didn't work for me. Fields I created from taxonomy using the checkbox widget didn't display in the views module as fields or filters.

#1

doc2@drupalfr.org - May 14, 2008 - 10:03

subscribing

 
 

Drupal is a registered trademark of Dries Buytaert.