You are here

function hs_taxonomy_form_field_ui_widget_type_form_alter in Hierarchical Select 7.3

Implements hook_form_FORMID_alter().

Alter the widget type form; dynamically add the Hierarchical Select Configuration form when it is needed.

File

modules/hs_taxonomy.module, line 42
Implementation of the Hierarchical Select API for the Taxonomy module.

Code

function hs_taxonomy_form_field_ui_widget_type_form_alter(&$form, &$form_state) {
  form_load_include($form_state, 'inc', 'hierarchical_select', 'includes/common');

  // Alter the widget type select: configure #ajax so that we can respond to
  // changes in its value: whenever it is set to "taxonomy_hs", we add the HS
  // config UI.
  $form['basic']['widget_type']['#ajax'] = array(
    'event' => 'change',
    'callback' => 'hs_taxonomy_field_ui_widget_settings_ajax',
    'wrapper' => 'hs-config-replace',
    'method' => 'replace',
  );
  $current_widget_type = isset($form_state['input']['widget_type']) ? $form_state['input']['widget_type'] : $form_state['build_info']['args'][0]['widget']['type'];
  if ($current_widget_type == 'taxonomy_hs') {
    $field = field_info_field($form['#field_name']);
    if (!empty($field['settings']['allowed_values'][0]['vocabulary'])) {
      $vocabulary = taxonomy_vocabulary_machine_name_load($field['settings']['allowed_values'][0]['vocabulary']);
    }
    $vid = isset($vocabulary->vid) ? $vocabulary->vid : NULL;
    $save_lineage = isset($vocabulary->hierarchy) ? (int) ($vocabulary->hierarchy == 2) : 0;
    $instance = field_info_instance($form['#entity_type'], $form['#field_name'], $form['#bundle']);

    // Add the Hierarchical Select config form.
    $module = 'hs_taxonomy';
    $params = array(
      'vid' => $vid,
      'exclude_tid' => NULL,
      'root_term' => NULL,
      'allowed_max_depth' => 0,
    );
    $config_id = hs_taxonomy_get_config_id($form['#field_name']);
    $defaults = array(
      // Enable the save_lineage setting by default if the multiple parents
      // vocabulary option is enabled.
      'save_lineage' => $save_lineage,
      'editability' => array(
        'max_levels' => _hs_taxonomy_hierarchical_select_get_depth($vid),
      ),
    );
    $strings = array(
      'hierarchy' => t('taxonomy_vocabulary'),
      'hierarchies' => t('vocabularies'),
      'item' => t('term'),
      'items' => t('terms'),
      'item_type' => t('term type'),
      'entity' => t('node'),
      'entities' => t('nodes'),
    );
    $max_hierarchy_depth = _hs_taxonomy_hierarchical_select_get_depth($vid);
    $preview_is_required = $instance['required'] == 1;
    $form['hs'] = hierarchical_select_common_config_form($module, $params, $config_id, $defaults, $strings, $max_hierarchy_depth, $preview_is_required);
    if (!module_exists('taxonomy_entity_index')) {

      // Only allow person to select nodes.
      $form['hs']['entity_count']['settings']['entity_types'] = array(
        'node' => $form['hs']['entity_count']['settings']['entity_types']['node'],
      );
      $form['hs']['entity_count']['settings']['#collapsed'] = FALSE;
      $form['hs']['entity_count']['settings']['entity_types']['node']['#collapsed'] = FALSE;
      $form['hs']['entity_count']['#description'] = '<p>' . t('You can extend this functionality to other entities if you install !taxonomy_entity_index.', array(
        '!taxonomy_entity_index' => l('Taxonomy entity index', 'https://www.drupal.org/project/taxonomy_entity_index'),
      )) . '</p>';
    }

    // Make the config form AJAX-updateable.
    $form['hs'] += array(
      '#prefix' => '<div id="hs-config-replace">',
      '#suffix' => '</div>',
    );

    // Add the submit handler for the Hierarchical Select config form. Make
    // sure it is executed first.
    $form['#hs_common_config_form_parents'] = array(
      'hs',
    );
    array_unshift($form['#submit'], 'hierarchical_select_common_config_form_submit');

    // Add a submit handler for HS Taxonomy that will update the field
    // settings when necessary.
    // @see hs_taxonomy_field_settings_submit() for details.
    $form['#submit'][] = 'hs_taxonomy_field_settings_submit';
  }
  else {
    $form['hs'] = array(
      '#prefix' => '<div id="hs-config-replace">',
      '#suffix' => '</div>',
    );
  }
}