How would you generate a tree with a render array / the form API? I've been trying variations of the following and also running it through term_reference_tree_process_checkbox_tree(), but I can't seem to find a way to get it to generate or display the tree.

	array(
		'#title' => 'Available Services',
		'#type' => 'taxonomy_term_reference',
		'#vocabulary' => 6,
		'#parent_tid' => 0,
		'#value_key' => 'tid',
	),

Comments

okj579’s picture

bump

bartk’s picture

Status: Active » Postponed (maintainer needs more info)

Try changing #type to checkbox_tree and let me know if that works.

owntheweb’s picture

ooO! I think this issue addresses close to what I need on a similar challenge. I'm making a custom form that generates lesson nodes (as part of a migration process - can't use standard form) and using your suggestion and some info on the first post I created this:

<?php
function lesson_mover($form, &$form_state) {
//...

	$form['lesson_standards'] = array(
		'#title' => 'Subject Areas/Standards',
		'#type' => 'checkbox_tree',
		'#vocabulary' => 5,
		'#parent_tid' => 0,
		'#value_key' => 'tid',
	);

//...
?>

That gets me a nice expanded radiobutton list with the hierarchy of my vocabulary. Cool! (let me know if that helps answer this issue that's been dead for a while)

Bonus challenges:
Any thoughts on how I can make my form element a list of checkboxes for multiple values to be selected, make only the last leaf selectable, and make it show in one of those excellent expanding trees?

Thanks for the added assist in advance. :D

Chris

owntheweb’s picture

Title: Render Array » getting closer...

Regarding the "bonus challenges", this works!:

<?php
//...
$term_reference_tree_path = drupal_get_path('module', 'term_reference_tree');
$form['lesson_standards'] = array(
	'#title' => 'Subject Areas/Standards',
	'#type' => 'checkbox_tree',
	'#max_choices' => 999,
	'#leaves_only' => 1,
	'#start_minimized' => TRUE,
	'#depth' => 0,
	'#vocabulary' => 5,
	'#parent_tid' => 0,
	'#value_key' => 'tid',
	'#select_parents' => TRUE,
	'#attached' => array(
		'js' => array($term_reference_tree_path . '/term_reference_tree.js'),
		'css' => array($term_reference_tree_path . '/term_reference_tree.css')
	),
	'#attributes' => array('class' => array('field-widget-term-reference-tree')),
);
//...
?>
owntheweb’s picture

Title: getting closer... » Render Taxonomy Vocabulary in Custom Form

Sorry, I accidentally changed the title earlier...

owntheweb’s picture

Hmmmm. This doesn't validate though when I submit the form, highlighting my tree in red once I select an option (validates fine if empty):

Warning: Illegal offset type in isset or empty in _form_validate() (line 1298 of /my/server/root/html/includes/form.inc).
An illegal choice has been detected. Please contact the site administrator.

Even if I add a custom validate function:

<?php
//...
	'#element_validate' => array('_lesson_mover_standards_validate'),
//...

function _lesson_mover_standards_validate(&$element, &$form_state) {
	//do nothing for now...
}

?>

Any thoughts on what could be wrong? I'll keep digging...

EDIT:

In form.inc at line 1297-1298:

<?php
//...
          foreach ($value as $v) {
            if (!isset($options[$v])) {
//...
?>

I can see why the error is happening. Here's what $v looks like:

Array
(
    [186] => Array
        (
            [186-children] => Array
                (
                    [187] => Array
                        (
                            [187-children] => Array
                                (
                                    [188] => Array
                                        (
                                            [188] => 188
                                        )

                                )

                        )

                )

        )

)

Any thoughts on how to get around this oddness? :S

Thanks again,

owntheweb’s picture

Hrmmm. I'm still stuck on this one. Any ideas? Thanks again,

mikran’s picture

Status: Postponed (maintainer needs more info) » Fixed

Here is fixed version of bonus challenge, this should fix the error from comment #6.

//...
$term_reference_tree_path = drupal_get_path('module', 'term_reference_tree');
$form['lesson_standards'] = array(
    '#title' => 'Subject Areas/Standards',
    '#type' => 'checkbox_tree',
    '#max_choices' => 999,
    '#leaves_only' => 1,
    '#start_minimized' => TRUE,
    '#depth' => 0,
    '#vocabulary' => 5,
    '#parent_tid' => 0,
    '#value_key' => 'tid',
    '#select_parents' => TRUE,
    '#attached' => array(
        'js' => array($term_reference_tree_path . '/term_reference_tree.js'),
        'css' => array($term_reference_tree_path . '/term_reference_tree.css')
    ),
    '#attributes' => array('class' => array('field-widget-term-reference-tree')),
    '#element_validate' => array('_term_reference_tree_widget_validate'),
    '#value' => array(),
);
//...

Added rows explained:

'#element_validate' => array('_term_reference_tree_widget_validate')

This custom element_validate function is basically a workaround for the validation error. From the function comments: "This function sets the value of the tree widgets into a form that Drupal can understand, and also checks if the field is required and has been left empty.".

'#value' => array(),

In addition to #element_validate #value is also needed and for the same reason I'd imagine. _term_reference_tree_widget_validate overwrites #value so it doesn't matter too much what the initial value is.

All the required element properties can be found from function term_reference_tree_field_widget_form(). Obviously these things could be improved to make it easier to use the widget as form API element.

owntheweb’s picture

Aha! I knew I was missing something. I tried the '#element_validate' but didn't have the '#value' in place at the same time.

Thanks so much for your help! This resolved the error issue I was running into when submitting custom form.

:D :D :D :D :D

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Minor edit