term_reference_tree.widget.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. <?php
  2. /**
  3. * Implements hook_field_widget_info().
  4. */
  5. function term_reference_tree_field_widget_info() {
  6. return array(
  7. 'term_reference_tree' => array(
  8. 'label' => t('Term reference tree'),
  9. 'field types' => array('taxonomy_term_reference'),
  10. 'behaviors' => array(
  11. 'multiple values' => FIELD_BEHAVIOR_CUSTOM,
  12. 'default value' => FIELD_BEHAVIOR_DEFAULT,
  13. ),
  14. 'settings' => array(
  15. 'start_minimized' => 0,
  16. 'leaves_only' => 0,
  17. 'filter_view' => '',
  18. 'select_parents' => 0,
  19. 'cascading_selection' => 0,
  20. 'track_list' => 0,
  21. 'token_display' => '',
  22. 'parent_term_id' => '',
  23. 'max_depth' => '',
  24. ),
  25. ),
  26. );
  27. }
  28. /**
  29. * Implements hook_field_widget_settings_form().
  30. */
  31. function term_reference_tree_field_widget_settings_form($field, $instance) {
  32. $widget = $instance['widget'];
  33. $settings = $widget['settings'];
  34. $form = array();
  35. if ($widget['type'] == 'term_reference_tree') {
  36. $form['start_minimized'] = array(
  37. '#type' => 'checkbox',
  38. '#title' => t('Start minimized'),
  39. '#description' => t('Make the tree appear minimized on the form by default'),
  40. '#default_value' => $settings['start_minimized'],
  41. '#return_value' => 1,
  42. );
  43. $form['leaves_only'] = array(
  44. '#type' => 'checkbox',
  45. '#title' => t('Leaves only'),
  46. '#description' => t("Don't allow the user to select items that have children"),
  47. '#default_value' => $settings['leaves_only'],
  48. '#return_value' => 1,
  49. );
  50. $form['select_parents'] = array(
  51. '#type' => 'checkbox',
  52. '#title' => t('Select parents automatically'),
  53. '#description' => t("When turned on, this option causes the widget to automatically select the ancestors of all selected items. In Leaves Only mode, the parents will be added invisibly to the selected value. <em>This option is only valid if an unlimited number of values can be selected.</em>"),
  54. '#default_value' => $settings['select_parents'],
  55. '#element_validate' => array('_term_reference_tree_select_parents_validate'),
  56. '#return_value' => 1,
  57. );
  58. $form['cascading_selection'] = array(
  59. '#type' => 'checkbox',
  60. '#title' => t('Cascading selection'),
  61. '#description' => t('On parent selection, automatically select children if none were selected. Some may then be manually unselected. In the same way, on parent unselection, unselect children if all were selected. <em>This option is only valid if an unlimited number of values can be selected.</em>'),
  62. '#default_value' => $settings['cascading_selection'],
  63. '#element_validate' => array('_term_reference_tree_cascading_selection_validate'),
  64. '#return_value' => 1,
  65. );
  66. if (module_exists('views')) {
  67. $views = views_get_all_views();
  68. $options = array('' => 'none');
  69. foreach ($views as $name => $view) {
  70. if ($view->base_table == 'taxonomy_term_data') {
  71. foreach ($view->display as $display) {
  72. $options["$name:{$display->id}"] = "{$view->human_name}: {$display->display_title}";
  73. }
  74. }
  75. }
  76. $form['filter_view'] = array(
  77. '#type' => 'select',
  78. '#title' => 'Filter by view',
  79. '#description' => t("Filter the available options based on whether they appear in the selected view."),
  80. '#default_value' => $settings['filter_view'],
  81. '#options' => $options,
  82. );
  83. }
  84. else {
  85. $form['filter_view'] = array(
  86. '#type' => 'hidden',
  87. '#value' => $settings['filter_view'],
  88. );
  89. }
  90. if (module_exists('token')) {
  91. $form['token_display'] = array(
  92. '#type' => 'textarea',
  93. '#title' => 'Custom Term Label',
  94. '#description' => t("Use tokens to change the term labels for the checkboxes and/or radio buttons. Leave this field blank to use the term name."),
  95. '#default_value' => $settings['token_display'],
  96. );
  97. $form['tokens_list'] = array(
  98. '#theme' => 'token_tree',
  99. '#token_types' => array('term'),
  100. );
  101. }
  102. else {
  103. $form['token_display'] = array(
  104. '#type' => 'hidden',
  105. '#value' => $settings['token_display'],
  106. );
  107. }
  108. $form['track_list'] = array(
  109. '#type' => 'checkbox',
  110. '#title' => t('Track list'),
  111. '#description' => t('Track what the user has chosen in a list below the tree. Useful when the tree is large, with many levels.'),
  112. '#default_value' => $settings['track_list'],
  113. '#return_value' => 1,
  114. );
  115. $form['max_depth'] = array(
  116. '#type' => 'textfield',
  117. '#title' => t('Maximum Depth'),
  118. '#description' => t("Only show items up to this many levels deep."),
  119. '#default_value' => $settings['max_depth'],
  120. '#size' => 2,
  121. '#return_value' => 1,
  122. );
  123. $form['parent_term_id'] = array(
  124. '#type' => 'textfield',
  125. '#title' => t('Parent Term ID'),
  126. '#description' => t("Only show items underneath the taxonomy term with this ID number. Leave this field blank to not limit terms by parent."),
  127. '#default_value' => $settings['parent_term_id'],
  128. '#size' => 8,
  129. '#return_value' => 1,
  130. );
  131. }
  132. return $form;
  133. }
  134. /**
  135. * Themes the term tree display (as opposed to the select widget).
  136. */
  137. function theme_term_tree_list($variables) {
  138. $element = &$variables['element'];
  139. $data = &$element['#data'];
  140. $tree = array();
  141. // For each selected term:
  142. foreach ($data as $item) {
  143. // Loop if the term ID is not zero:
  144. $values = array();
  145. $tid = $item['tid'];
  146. $original_tid = $tid;
  147. while ($tid != 0) {
  148. // Unshift the term onto an array
  149. array_unshift($values, $tid);
  150. // Repeat with parent term
  151. $tid = _term_reference_tree_get_parent($tid);
  152. }
  153. $current = &$tree;
  154. // For each term in the above array:
  155. foreach ($values as $tid) {
  156. // current[children][term_id] = new array
  157. if (!isset($current['children'][$tid])) {
  158. $current['children'][$tid] = array('selected' => FALSE);
  159. }
  160. // If this is the last value in the array, tree[children][term_id][selected] = true
  161. if ($tid == $original_tid) {
  162. $current['children'][$tid]['selected'] = TRUE;
  163. }
  164. $current['children'][$tid]['tid'] = $tid;
  165. $current = &$current['children'][$tid];
  166. }
  167. }
  168. $output = "<div class='term-tree-list'>";
  169. $output .= _term_reference_tree_output_list_level($element, $tree);
  170. $output .= "</div>";
  171. return $output;
  172. }
  173. /**
  174. * Helper function to output a single level of the term reference tree
  175. * display.
  176. */
  177. function _term_reference_tree_output_list_level(&$element, &$tree) {
  178. if (isset($tree['children']) && is_array($tree['children']) && count($tree['children'] > 0)) {
  179. $output = '<ul class="term">';
  180. $settings = $element['#display']['settings'];
  181. $tokens_selected = $settings['token_display_selected'];
  182. $tokens_unselected = ($settings['token_display_unselected'] != '') ? $settings['token_display_unselected'] : $tokens_selected;
  183. $taxonomy_term_info = entity_get_info('taxonomy_term');
  184. foreach ($tree['children'] as &$item) {
  185. $term = $taxonomy_term_info['load hook']($item['tid']);
  186. $uri = $taxonomy_term_info['uri callback']($term);
  187. $uri['options']['html'] = TRUE;
  188. $class = $item['selected'] ? 'selected' : 'unselected';
  189. $output .= "<li class='$class'>";
  190. if ($tokens_selected != '' && module_exists('token')) {
  191. $replace = $item['selected'] ? $tokens_selected : $tokens_unselected;
  192. $output .= token_replace($replace, array('term' => $term), array('clear' => TRUE));
  193. }
  194. else {
  195. $output .= l(filter_xss(entity_label('taxonomy_term', $term)), $uri['path'], $uri['options']);
  196. }
  197. if (isset($item['children'])) {
  198. $output .= _term_reference_tree_output_list_level($element, $item);
  199. }
  200. $output .= "</li>";
  201. }
  202. $output .= '</ul>';
  203. return $output;
  204. }
  205. }
  206. /**
  207. * Makes sure that cardinality is unlimited if auto-select parents is enabled.
  208. */
  209. function _term_reference_tree_select_parents_validate($element, &$form_state) {
  210. if ($form_state['values']['instance']['widget']['settings']['select_parents'] == 1 && $form_state['values']['field']['cardinality'] != -1) {
  211. // This is pretty wonky syntax for the field name in form_set_error, but it's
  212. // correct.
  213. form_set_error('field][cardinality', t('You must select an Unlimited number of values if Select Parents Automatically is enabled.'));
  214. }
  215. }
  216. /**
  217. * Makes sure that cardinality is unlimited if cascading selection is enabled.
  218. */
  219. function _term_reference_tree_cascading_selection_validate($element, &$form_state) {
  220. if ($form_state['values']['instance']['widget']['settings']['cascading_selection'] == 1 && $form_state['values']['field']['cardinality'] != -1) {
  221. // This is pretty wonky syntax for the field name in form_set_error, but it's
  222. // correct.
  223. form_set_error('field][cardinality', t('You must select an Unlimited number of values if Cascading selection is enabled.'));
  224. }
  225. }
  226. /**
  227. * Process the checkbox_tree widget.
  228. *
  229. * This function processes the checkbox_tree widget.
  230. *
  231. * @param $element
  232. * The element to be drawn.$element['#field_name']
  233. * @param $form_state
  234. * The form state.
  235. *
  236. * @return
  237. * The processed element.
  238. */
  239. function term_reference_tree_process_checkbox_tree($element, $form_state) {
  240. if (is_array($form_state)) {
  241. if (!empty($element['#max_choices']) && $element['#max_choices'] != '-1') {
  242. drupal_add_js(array('term_reference_tree' => array('trees' => array($element['#id'] => array('max_choices' => $element['#max_choices'])))), 'setting');
  243. }
  244. $allowed = '';
  245. if ($element['#filter_view'] != '') {
  246. $allowed = _term_reference_tree_get_allowed_values($element['#filter_view']);
  247. }
  248. $value = !empty($element['#default_value']) ? $element['#default_value'] : array();
  249. if (empty($element['#options'])) {
  250. $element['#options_tree'] = _term_reference_tree_get_term_hierarchy($element['#parent_tid'], $element['#vocabulary']->vid, $allowed, $element['#filter_view'], '', $value);
  251. $required = $element['#required'];
  252. if ($element['#max_choices'] == 1 && !$required) {
  253. array_unshift($element['#options_tree'], (object) array(
  254. 'tid' => '',
  255. 'name' => 'N/A',
  256. 'depth' => 0,
  257. 'vocabulary_machine_name' => $element['#vocabulary']->machine_name,
  258. ));
  259. }
  260. $element['#options'] = _term_reference_tree_get_options($element['#options_tree'], $allowed, $element['#filter_view']);
  261. }
  262. $terms = !empty($element['#options_tree']) ? $element['#options_tree'] : array();
  263. $max_choices = !empty($element['#max_choices']) ? $element['#max_choices'] : 1;
  264. if (array_key_exists('#select_parents', $element) && $element['#select_parents']) {
  265. $element['#attributes']['class'][] = 'select-parents';
  266. }
  267. if ($max_choices != 1) {
  268. $element['#tree'] = TRUE;
  269. }
  270. $tree = new stdClass();
  271. $tree->children = $terms;
  272. $element[] = _term_reference_tree_build_level($element, $tree, $form_state, $value, $max_choices, array(), 1);
  273. // Add a track list element?
  274. $track_list = !empty($element['#track_list']) && $element['#track_list'];
  275. if ($track_list) {
  276. $element[] = array(
  277. '#type' => 'checkbox_tree_track_list',
  278. '#max_choices' => $max_choices,
  279. );
  280. }
  281. }
  282. return $element;
  283. }
  284. /**
  285. * Returns HTML for a checkbox_tree form element.
  286. *
  287. * @param $variables
  288. * An associative array containing:
  289. * - element: An associative array containing the properties of the element.
  290. *
  291. * @ingroup themeable
  292. */
  293. function theme_checkbox_tree($variables) {
  294. $element = $variables['element'];
  295. $element['#children'] = drupal_render_children($element);
  296. $attributes = array();
  297. if (isset($element['#id'])) {
  298. $attributes['id'] = $element['#id'];
  299. }
  300. $attributes['class'][] = 'term-reference-tree';
  301. if (form_get_error($element)) {
  302. $attributes['class'][] = 'error';
  303. }
  304. if (!empty($element['#required'])) {
  305. $attributes['class'][] = 'required';
  306. }
  307. if (array_key_exists('#start_minimized', $element) && $element['#start_minimized']) {
  308. $attributes['class'][] = "term-reference-tree-collapsed";
  309. }
  310. if (array_key_exists('#cascading_selection', $element) && $element['#cascading_selection']) {
  311. $attributes['class'][] = "term-reference-tree-cascading-selection";
  312. }
  313. $add_track_list = FALSE;
  314. if (array_key_exists('#track_list', $element) && $element['#track_list']) {
  315. $attributes['class'][] = "term-reference-tree-track-list-shown";
  316. $add_track_list = TRUE;
  317. }
  318. if (!empty($element['#attributes']['class'])) {
  319. $attributes['class'] = array_merge($attributes['class'], $element['#attributes']['class']);
  320. }
  321. return
  322. '<div' . drupal_attributes($attributes) . '>'
  323. . (!empty($element['#children']) ? $element['#children'] : '')
  324. . '</div>';
  325. }
  326. /**
  327. * This function prints a list item with a checkbox and an unordered list
  328. * of all the elements inside it.
  329. */
  330. function theme_checkbox_tree_level($variables) {
  331. $element = $variables['element'];
  332. $sm = '';
  333. if (array_key_exists('#level_start_minimized', $element) && $element['#level_start_minimized']) {
  334. $sm = " style='display: none;'";
  335. }
  336. $max_choices = 0;
  337. if (array_key_exists('#max_choices', $element)) {
  338. $max_choices = $element['#max_choices'];
  339. }
  340. $output = "<ul class='term-reference-tree-level '$sm>";
  341. $children = element_children($element);
  342. foreach ($children as $child) {
  343. $output .= "<li>";
  344. $output .= drupal_render($element[$child]);
  345. $output .= "</li>";
  346. }
  347. $output .= "</ul>";
  348. return $output;
  349. }
  350. /**
  351. * This function prints a single item in the tree, followed by that item's children
  352. * (which may be another checkbox_tree_level).
  353. */
  354. function theme_checkbox_tree_item($variables) {
  355. $element = $variables['element'];
  356. $children = element_children($element);
  357. $output = "";
  358. $sm = $element['#level_start_minimized'] ? ' term-reference-tree-collapsed' : '';
  359. if (is_array($children) && count($children) > 1) {
  360. $output .= "<div class='term-reference-tree-button$sm'></div>";
  361. }
  362. elseif (!$element['#leaves_only']) {
  363. $output .= "<div class='no-term-reference-tree-button'></div>";
  364. }
  365. foreach ($children as $child) {
  366. $output .= drupal_render($element[$child]);
  367. }
  368. return $output;
  369. }
  370. /**
  371. * This function prints a label that cannot be selected.
  372. */
  373. function theme_checkbox_tree_label($variables) {
  374. $element = $variables['element'];
  375. $output = "<div class='parent-term'>" . $element['#value'] . "</div>";
  376. return $output;
  377. }
  378. /**
  379. * Shows a list of items that have been checked.
  380. * The display happens on the client-side.
  381. * Use this function to theme the element's label,
  382. * and the "nothing selected" message.
  383. *
  384. * @param $variables
  385. * Variables available for theming.
  386. */
  387. function theme_checkbox_tree_track_list($variables) {
  388. // Should the label be singular or plural? Depends on cardinality of term field.
  389. static $nothingselected;
  390. if (!$nothingselected) {
  391. $nothingselected = t('[Nothing selected]');
  392. // Add the "Nothing selected" text. To style it, replace it with whatever you want.
  393. // Could do this with a file instead.
  394. drupal_add_js(
  395. 'var termReferenceTreeNothingSelectedText = "' . $nothingselected . '";',
  396. 'inline'
  397. );
  398. }
  399. $label = format_plural(
  400. $variables['element']['#max_choices'],
  401. 'Selected item (click the item to uncheck it)',
  402. 'Selected items (click an item to uncheck it)'
  403. );
  404. $output = '<div class="term-reference-track-list-container">';
  405. $output .= '<div class="term-reference-track-list-label">' . $label . '</div>';
  406. $output .= '<ul class="term-reference-tree-track-list"><li class="term_ref_tree_nothing_message">' . $nothingselected . '</li></ul>';
  407. $output .= '</div>';
  408. return $output;
  409. }
  410. /**
  411. * Implements hook_field_widget_form().
  412. */
  413. function term_reference_tree_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  414. $settings = $instance['widget']['settings'];
  415. $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
  416. $path = drupal_get_path('module', 'term_reference_tree');
  417. $value_key = key($field['columns']);
  418. $type = $instance['widget']['type'];
  419. $default_value = array();
  420. foreach ($items as $item) {
  421. $key = $item[$value_key];
  422. if ($key === 0) {
  423. $default_value[$key] = '0';
  424. }
  425. else {
  426. $default_value[$key] = $key;
  427. }
  428. }
  429. $multiple = $field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED;
  430. $properties = array();
  431. if (!array_key_exists('#value', $element)) {
  432. $element['#value'] = array();
  433. }
  434. // A switch statement, in case we ever add more widgets to this module.
  435. switch ($instance['widget']['type']) {
  436. case 'term_reference_tree':
  437. $element['#attached']['js'] = array($path . '/term_reference_tree.js');
  438. $element['#attached']['css'] = array($path . '/term_reference_tree.css');
  439. $element['#type'] = 'checkbox_tree';
  440. $element['#default_value'] = $multiple ? $default_value : array(reset($default_value) => reset($default_value));
  441. $element['#max_choices'] = $field['cardinality'];
  442. $element['#max_depth'] = $settings['max_depth'];
  443. $element['#start_minimized'] = $settings['start_minimized'];
  444. $element['#leaves_only'] = $settings['leaves_only'];
  445. $element['#filter_view'] = module_exists('views') ? $settings['filter_view'] : '';
  446. $element['#select_parents'] = $settings['select_parents'];
  447. $element['#cascading_selection'] = $settings['cascading_selection'];
  448. $element['#track_list'] = $settings['track_list'];
  449. $element['#parent_tid'] = $settings['parent_term_id'] ? $settings['parent_term_id'] : $field['settings']['allowed_values'][0]['parent'];
  450. $element['#vocabulary'] = $vocabulary;
  451. $element['#token_display'] = module_exists('token') ? $settings['token_display'] : '';
  452. break;
  453. }
  454. $element += array(
  455. '#value_key' => $value_key,
  456. '#element_validate' => array('_term_reference_tree_widget_validate'),
  457. '#properties' => $properties,
  458. );
  459. return $element;
  460. }
  461. /**
  462. * Validates the term reference tree widgets.
  463. *
  464. * This function sets the value of the tree widgets into a form that Drupal
  465. * can understand, and also checks if the field is required and has been
  466. * left empty.
  467. *
  468. * @param $element
  469. * The element to be validated.
  470. * @param $form_state
  471. * The state of the form.
  472. *
  473. * @return
  474. * The validated element.
  475. */
  476. function _term_reference_tree_widget_validate(&$element, &$form_state) {
  477. $items = _term_reference_tree_flatten($element, $form_state);
  478. $value = array();
  479. if ($element['#max_choices'] != 1) {
  480. foreach ($items as $child) {
  481. if (!empty($child['#value'])) {
  482. array_push($value, array($element['#value_key'] => $child['#value']));
  483. // If the element is leaves only and select parents is on, then automatically
  484. // add all the parents of each selected value.
  485. if ($element['#select_parents'] && $element['#leaves_only']) {
  486. foreach ($child['#parent_values'] as $parent_tid) {
  487. if (!in_array(array($element['#value_key'] => $parent_tid), $value)) {
  488. array_push($value, array($element['#value_key'] => $parent_tid));
  489. }
  490. }
  491. }
  492. }
  493. }
  494. }
  495. else {
  496. // If it's a tree of radio buttons, they all have the same value, so we can just
  497. // grab the value of the first one.
  498. if (count($items) > 0) {
  499. $child = reset($items);
  500. if (!empty($child['#value'])) {
  501. array_push($value, array($element['#value_key'] => $child['#value']));
  502. }
  503. }
  504. }
  505. if ($element['#required'] && empty($value)) {
  506. // The title is already check_plained so it's appropriate to use !.
  507. form_error($element, t('!name field is required.', array('!name' => $element['#title'])));
  508. }
  509. form_set_value($element, $value, $form_state);
  510. return $element;
  511. }
  512. /**
  513. * Returns an array of allowed values defined by the given view.
  514. *
  515. * @param $filter
  516. * A view, in the format VIEWNAME:DISPLAYNAME
  517. *
  518. * @return
  519. * An array of term IDs (tid => true) returned by the view.
  520. */
  521. function _term_reference_tree_get_allowed_values($filter) {
  522. $viewname = "";
  523. $displayname = "";
  524. $allowed = array();
  525. if (module_exists('views') && $filter != '') {
  526. list($viewname, $displayname) = explode(":", $filter);
  527. $view = views_get_view($viewname);
  528. if (is_object($view)) {
  529. if ($view->access($displayname)) {
  530. // Save the page title first, since execute_display() will reset this to the display title.
  531. $title = drupal_get_title();
  532. $view->execute_display($displayname);
  533. $title = drupal_set_title($title, PASS_THROUGH);
  534. foreach ($view->result as $item) {
  535. $allowed[$item->tid] = TRUE;
  536. }
  537. }
  538. else {
  539. drupal_set_message("Cannot access view for term reference tree widget.", 'warning');
  540. }
  541. }
  542. else {
  543. drupal_set_message("Term reference tree: no view named '$viewname'", 'warning');
  544. }
  545. }
  546. return $allowed;
  547. }
  548. /**
  549. * Builds a single item in the term reference tree widget.
  550. *
  551. * This function returns an element with a checkbox for a single taxonomy term.
  552. * If that term has children, it appends checkbox_tree_level element that
  553. * contains the children. It is meant to be called recursively when the widget
  554. * is built.
  555. *
  556. * @param $element
  557. * The main checkbox_tree element.
  558. * @param $term
  559. * A taxonomy term object. $term->children should be an array of the term
  560. * objects that are that term's children.
  561. * @param $form_state
  562. * The form state.
  563. * @param $value
  564. * The value of the element.
  565. * @param $max_choices
  566. * The maximum number of allowed selections.
  567. *
  568. * @return
  569. * A completed checkbox_tree_item element, which contains a checkbox and
  570. * possibly a checkbox_tree_level element as well.
  571. */
  572. function _term_reference_tree_build_item($element, $term, $form_state, $value, $max_choices, $parent_tids, $parent, $depth) {
  573. $start_minimized = FALSE;
  574. if (array_key_exists('#start_minimized', $element)) {
  575. $start_minimized = $element['#start_minimized'];
  576. }
  577. $leaves_only = FALSE;
  578. if (array_key_exists('#leaves_only', $element)) {
  579. $leaves_only = $element['#leaves_only'];
  580. }
  581. $t = NULL;
  582. if (module_exists('locale') && !empty($term->tid)) {
  583. $t = taxonomy_term_load($term->tid);
  584. $term_name = entity_label('taxonomy_term', $t);
  585. }
  586. else {
  587. $term_name = $term->name;
  588. }
  589. $container = array(
  590. '#type' => 'checkbox_tree_item',
  591. '#max_choices' => $max_choices,
  592. '#leaves_only' => $leaves_only,
  593. '#term_name' => $term_name,
  594. '#level_start_minimized' => FALSE,
  595. '#depth' => $depth,
  596. );
  597. if (!$element['#leaves_only'] || count($term->children) == 0) {
  598. $name = "edit-" . str_replace('_', '-', $element['#field_name']);
  599. $e = array(
  600. '#type' => ($max_choices == 1) ? 'radio' : 'checkbox',
  601. '#title' => $term_name,
  602. '#on_value' => $term->tid,
  603. '#off_value' => 0,
  604. '#return_value' => $term->tid,
  605. '#parent_values' => $parent_tids,
  606. '#default_value' => isset($value[$term->tid]) ? $term->tid : NULL,
  607. '#attributes' => isset($element['#attributes']) ? $element['#attributes'] : NULL,
  608. '#ajax' => isset($element['#ajax']) ? $element['#ajax'] : NULL,
  609. );
  610. if ($element['#token_display'] != '' && module_exists('token')) {
  611. if (!$t) {
  612. $t = taxonomy_term_load($term->tid);
  613. }
  614. $e['#title'] = token_replace($element['#token_display'], array('term' => $t), array('clear' => TRUE));
  615. }
  616. if ($e['#type'] == 'radio') {
  617. $parents_for_id = array_merge($element['#parents'], array($term->tid));
  618. $e['#id'] = drupal_html_id('edit-' . implode('-', $parents_for_id));
  619. $e['#parents'] = $element['#parents'];
  620. }
  621. }
  622. else {
  623. $e = array(
  624. '#type' => 'checkbox_tree_label',
  625. '#value' => $term_name,
  626. );
  627. }
  628. $container[$term->tid] = $e;
  629. if (($depth + 1 <= $element['#max_depth'] || !$element['#max_depth']) && property_exists($term, 'children') && count($term->children) > 0) {
  630. $parents = $parent_tids;
  631. $parents[] = $term->tid;
  632. $container[$term->tid . '-children'] = _term_reference_tree_build_level($element, $term, $form_state, $value, $max_choices, $parents, $depth + 1);
  633. $container['#level_start_minimized'] = $container[$term->tid . '-children']['#level_start_minimized'];
  634. }
  635. return $container;
  636. }
  637. /**
  638. * Builds a level in the term reference tree widget.
  639. *
  640. * This function returns an element that has a number of checkbox_tree_item elements
  641. * as children. It is meant to be called recursively when the widget is built.
  642. *
  643. * @param $element
  644. * The main checkbox_tree element.
  645. * @param $term
  646. * A taxonomy term object. $term->children should be an array of the term
  647. * objects that are that term's children.
  648. * @param $form_state
  649. * The form state.
  650. * @param $value
  651. * The value of the element.
  652. * @param $max_choices
  653. * The maximum number of allowed selections.
  654. *
  655. * @return
  656. * A completed checkbox_tree_level element.
  657. */
  658. function _term_reference_tree_build_level($element, $term, $form_state, $value, $max_choices, $parent_tids, $depth) {
  659. $start_minimized = FALSE;
  660. if (array_key_exists('#start_minimized', $element)) {
  661. $start_minimized = $element['#start_minimized'];
  662. }
  663. $leaves_only = FALSE;
  664. if (array_key_exists('#leaves_only', $element)) {
  665. $leaves_only = $element['#leaves_only'];
  666. }
  667. $container = array(
  668. '#type' => 'checkbox_tree_level',
  669. '#max_choices' => $max_choices,
  670. '#leaves_only' => $leaves_only,
  671. '#start_minimized' => $start_minimized,
  672. '#depth' => $depth,
  673. );
  674. $container['#level_start_minimized'] = $depth > 1 && $element['#start_minimized'] && !($term->children_selected);
  675. foreach ($term->children as $child) {
  676. $container[$child->tid] = _term_reference_tree_build_item($element, $child, $form_state, $value, $max_choices, $parent_tids, $container, $depth);
  677. }
  678. return $container;
  679. }