You are here

function hser_field_widget_form in Hierarchical Select 7.3

Implements hook_field_widget_form().

File

modules/hser/hser.module, line 51
Allows hierarchical select to be used with entity reference fields.

Code

function hser_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  if ($field['settings']['target_type'] == 'taxonomy_term') {
    $vocabularies = $field['settings']['handler_settings']['target_bundles'];
    if (count($vocabularies) == 1 && ($vocabulary = taxonomy_vocabulary_machine_name_load(reset($vocabularies)))) {
      $default_value = array();
      foreach ($items as $item) {
        $default_value[] = $item['target_id'];
      }
      $element += array(
        '#type' => 'hierarchical_select',
        '#size' => 1,
        '#default_value' => $default_value,
        '#element_validate' => array(
          '_hser_element_validate',
        ),
        '#required' => $instance['required'],
        '#config' => array(
          'module' => 'hs_taxonomy',
          'params' => array(
            'vid' => $vocabulary->vid,
          ),
          'save_lineage' => FALSE,
          'enforce_deepest' => FALSE,
          'resizable' => FALSE,
          'level_labels' => array(
            'status' => FALSE,
          ),
          'dropbox' => array(
            'status' => $field['cardinality'] != 1,
            'limit' => $field['cardinality'],
          ),
          'editability' => array(
            'status' => $instance['widget']['settings']['editable'],
            'allow_new_levels' => TRUE,
            'max_levels' => 0,
          ),
          'entity_count' => array(
            'enabled' => 0,
            'require_entity' => 0,
            'settings' => array(
              'count_children' => 0,
              'entity_types' => array(),
            ),
          ),
          'render_flat_select' => FALSE,
        ),
      );
      return $element;
    }
  }

  // If we reach this point, we decided that hierarchical_select would not be
  // appropriate for some reason (not taxonomy, no vocabulary selected, etc).
  // So instead we fall back to a normal select widget.
  $instance['widget']['type'] = 'options_select';
  return options_field_widget_form($form, $form_state, $field, $instance, $langcode, $items, $delta, $element);
}