You are here

API.txt in Hierarchical Select 5.2

Same filename and directory in other branches
  1. 5.3 API.txt
  2. 5 API.txt
  3. 6.3 API.txt
  4. 7.3 API.txt
Form API usage
--------------
You have to make sure your form item is using the "hierarchical_select" form
element type:

  $form['select_some_term'] = array(
    '#type' => 'hierarchical_select',
    '#title' => t('Select the tag you wish to use.'),
    '#options' => $options, // Contains an array of tid - term name pairs.
    '#hierarchical_select_settings' => array(
      'module' => 'taxonomy',
      'save_lineage' => FALSE,
      'enforce_deepest' => FALSE,
      'all_option' => FALSE,
      'level_labels' => array(
        0 => t('Main category'),
        1 => t('Subcategory'),
      ),
      'params' => array(
        'vid' => $vid,
      ),
      'animation_delay' => 400,
      'dropbox_title' => t('All selections:'),
      'dropbox_limit' => 0,
    ),
    '#default_value' => '83',
  ); 

Now, let's explain what we see here:
1) We've set the #type property to "hierarchical_select" instead of "select".
2) There's a new property: #hierarchical_select_settings. This must be an
array. These are the items it can contain:

 - module (obligated)

   This will be passed through in the AHAH requests, to let the HS module know
   which module's hooks should be used.


 - save_lineage (optional, defaults to FALSE)
 
   Triggers the lineage saving functionality.


 - enforce_deepest (optional, defaults to FALSE)

   Triggers the enforcing of a selection in the deepest level.


 - all_option (optional, defaults to FALSE)

   Adds an "<all>" option, which will select all (non-special) values.


 - level_labels (optional)

   An array of labels, one per level. The label for the first level should be
   the value of key 0.
   When enforce_deepest is set to:
   - FALSE, then you can provide n level labels, with n the number of levels
   - TRUE, then you can provide only one level label.   


 - params (optional, may be necessary for some implementations)

   An array of parameters that will also be passed through in every AHAH
   request.
   e.g. In the case of taxonomy, this is the vocabulary id (vid). In case of
   content_taxonomy, there's two parameters: vid and the depth.


 - animation_delay (optional, defaults to 400)

   The delay of each animation (the drop in left and right animations), in ms.


 - dropbox_title (optional, defaults to "All selections:")

   The title of the dropbox. The dropbox is the area where all selected items
   are displayed when multiple select is enabled.


 - dropbox_limit (optional, defaults to 0, which means "no limit")

   Limit the number of items that can be added to the dropbox. So this allows
   you the restrict the number of items that can be selected when multiple
   select has been enabled.


Special values
--------------
- Ensure that your options don't have a "none" or "all" value, nor "label_\d+"
  values (the latter means "label_" followed by one or more digits). Your
  values should also not contain a pipe ("|"), since pipes are used to
  separate the selection of values that are sent back to the server in the
  callbacks.
- Valid 'empty' selections (i.e. if you want to set the #default_value
  property of your form item), are -1 and the empty array. The empty string is
  also considered valid, because Drupal core's Taxonomy module uses this as
  the empty selection.


Hooks
-----
1) hook_hierarchial_select_form_alter($form_id, &$form);

   This hook is absolutely optional! The sole reason for its existence, is to
   allow easy altering of forms of modules that don't support Hierarchical
   Select natively, to use the hierarchical_select form item.


2) hook_hierarchical_select_params();

   Returns the names of all parameters that should be present.


3) hook_hierarchical_select_root_level($params);

   Returns the root level of the hierarchy.


4) hook_hierarchical_select_children($parent, $params);

   Gets the children of $parent and returns them.


5) hook_hierarchical_select_lineage($item, $params);

   Calculates the lineage of $item and returns it. Necessary when the
   "enforce_deepest" option is enabled.


6) hook_hierarchical_select_valid_item($item, $params);

   Validates an item, returns TRUE if valid, FALSE if invalid.


7) hook_hierarchical_select_item_get_label($item, $params);

   Given a valid item, returns the label.

File

API.txt
View source
  1. Form API usage
  2. --------------
  3. You have to make sure your form item is using the "hierarchical_select" form
  4. element type:
  5. $form['select_some_term'] = array(
  6. '#type' => 'hierarchical_select',
  7. '#title' => t('Select the tag you wish to use.'),
  8. '#options' => $options, // Contains an array of tid - term name pairs.
  9. '#hierarchical_select_settings' => array(
  10. 'module' => 'taxonomy',
  11. 'save_lineage' => FALSE,
  12. 'enforce_deepest' => FALSE,
  13. 'all_option' => FALSE,
  14. 'level_labels' => array(
  15. 0 => t('Main category'),
  16. 1 => t('Subcategory'),
  17. ),
  18. 'params' => array(
  19. 'vid' => $vid,
  20. ),
  21. 'animation_delay' => 400,
  22. 'dropbox_title' => t('All selections:'),
  23. 'dropbox_limit' => 0,
  24. ),
  25. '#default_value' => '83',
  26. );
  27. Now, let's explain what we see here:
  28. 1) We've set the #type property to "hierarchical_select" instead of "select".
  29. 2) There's a new property: #hierarchical_select_settings. This must be an
  30. array. These are the items it can contain:
  31. - module (obligated)
  32. This will be passed through in the AHAH requests, to let the HS module know
  33. which module's hooks should be used.
  34. - save_lineage (optional, defaults to FALSE)
  35. Triggers the lineage saving functionality.
  36. - enforce_deepest (optional, defaults to FALSE)
  37. Triggers the enforcing of a selection in the deepest level.
  38. - all_option (optional, defaults to FALSE)
  39. Adds an "" option, which will select all (non-special) values.
  40. - level_labels (optional)
  41. An array of labels, one per level. The label for the first level should be
  42. the value of key 0.
  43. When enforce_deepest is set to:
  44. - FALSE, then you can provide n level labels, with n the number of levels
  45. - TRUE, then you can provide only one level label.
  46. - params (optional, may be necessary for some implementations)
  47. An array of parameters that will also be passed through in every AHAH
  48. request.
  49. e.g. In the case of taxonomy, this is the vocabulary id (vid). In case of
  50. content_taxonomy, there's two parameters: vid and the depth.
  51. - animation_delay (optional, defaults to 400)
  52. The delay of each animation (the drop in left and right animations), in ms.
  53. - dropbox_title (optional, defaults to "All selections:")
  54. The title of the dropbox. The dropbox is the area where all selected items
  55. are displayed when multiple select is enabled.
  56. - dropbox_limit (optional, defaults to 0, which means "no limit")
  57. Limit the number of items that can be added to the dropbox. So this allows
  58. you the restrict the number of items that can be selected when multiple
  59. select has been enabled.
  60. Special values
  61. --------------
  62. - Ensure that your options don't have a "none" or "all" value, nor "label_\d+"
  63. values (the latter means "label_" followed by one or more digits). Your
  64. values should also not contain a pipe ("|"), since pipes are used to
  65. separate the selection of values that are sent back to the server in the
  66. callbacks.
  67. - Valid 'empty' selections (i.e. if you want to set the #default_value
  68. property of your form item), are -1 and the empty array. The empty string is
  69. also considered valid, because Drupal core's Taxonomy module uses this as
  70. the empty selection.
  71. Hooks
  72. -----
  73. 1) hook_hierarchial_select_form_alter($form_id, &$form);
  74. This hook is absolutely optional! The sole reason for its existence, is to
  75. allow easy altering of forms of modules that don't support Hierarchical
  76. Select natively, to use the hierarchical_select form item.
  77. 2) hook_hierarchical_select_params();
  78. Returns the names of all parameters that should be present.
  79. 3) hook_hierarchical_select_root_level($params);
  80. Returns the root level of the hierarchy.
  81. 4) hook_hierarchical_select_children($parent, $params);
  82. Gets the children of $parent and returns them.
  83. 5) hook_hierarchical_select_lineage($item, $params);
  84. Calculates the lineage of $item and returns it. Necessary when the
  85. "enforce_deepest" option is enabled.
  86. 6) hook_hierarchical_select_valid_item($item, $params);
  87. Validates an item, returns TRUE if valid, FALSE if invalid.
  88. 7) hook_hierarchical_select_item_get_label($item, $params);
  89. Given a valid item, returns the label.