You are here

function hs_taxonomy_form_alter in Hierarchical Select 5.3

Same name and namespace in other branches
  1. 6.3 modules/hs_taxonomy.module \hs_taxonomy_form_alter()

Implementation of hook_form_alter().

File

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

Code

function hs_taxonomy_form_alter($form_id, &$form) {

  // Add per-vocabulary settings for Hierarchical Select.
  if ($form_id == 'taxonomy_form_vocabulary') {
    require_once drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';
    $vid = isset($form['vid']) ? $form['vid']['#value'] : NULL;

    // Don't add the per-vocabulary settings for Hierarchical Select when the
    // vocabulary still needs to be created.
    if ($vid == NULL) {
      return;
    }
    if (variable_get("taxonomy_hierarchical_select_{$vid}", 0)) {
      $form['tags']['#attributes']['disabled'] = TRUE;
      $form['tags']['#description'] = t("This setting is irrelevant when you're using Hierarchical Select.\n        <br />Use Hierarchical Select's %editability-settings instead.", array(
        '%editability-settings' => t('Editability settings'),
      ));
      $form['multiple']['#attributes']['disabled'] = TRUE;
      $form['multiple']['#description'] = t("This setting is managed by the Hierarchical Select configuration, by\n        the %enable-dropbox setting.", array(
        '%enable-dropbox' => t('Enable the dropbox'),
      ));
    }
    $split = array_search('weight', array_keys($form)) + 1;
    $first_part = array_slice($form, 0, $split);
    $second_part = array_slice($form, $split);
    $form = $first_part;
    $form['hierarchical_select_status'] = array(
      '#type' => 'checkbox',
      '#title' => '<strong>' . t('Use the Hierarchical Select form element for this vocabulary.') . '</strong>',
      '#default_value' => variable_get("taxonomy_hierarchical_select_{$vid}", 0),
      '#description' => t('When checked, the %free_tagging and %multiple_values settings will
        be managed by the Hierarchical Select configuration.', array(
        '%free_tagging' => t('Free tagging'),
        '%multiple_values' => t('Multiple values'),
      )),
    );

    // Add the Hierarchical Select config form.
    $module = 'hs_taxonomy';
    $params = array(
      'vid' => $vid,
      'exclude_tid' => NULL,
      'root_term' => NULL,
    );
    $config_id = "taxonomy-{$vid}";
    $vocabulary = taxonomy_get_vocabulary($vid);
    $defaults = array(
      // Enable the save_lineage setting by default if the multiple parents
      // vocabulary option is enabled.
      'save_lineage' => (int) ($vocabulary->hierarchy == 2),
      'editability' => array(
        'max_levels' => _hs_taxonomy_hierarchical_select_get_depth($vid),
      ),
    );
    $strings = array(
      'hierarchy' => t('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 = $vocabulary->required;
    $form['hierarchical_select'] = hierarchical_select_common_config_form($module, $params, $config_id, $defaults, $strings, $max_hierarchy_depth, $preview_is_required);

    // The forum selection requires that only the deepest term is saved!
    // See http://drupal.org/node/241766#comment-808464.
    if ($vid == variable_get('forum_nav_vocabulary', -1)) {
      $form['hierarchical_select']['save_lineage']['#value'] = 0;
      $form['hierarchical_select']['save_lineage']['#attributes'] = array(
        'disabled' => 'disabled',
      );
      $form['hierarchical_select']['save_lineage']['#description'] .= '<br />' . t('This is the vocabulary that will be used for forum navigation and it
        <strong>always</strong> requires the %dont_save_lineage setting to be
        set!', array(
        '%dont_save_lineage' => t('Save only the deepest term'),
      ));
    }

    // Add the the submit handler for the Hierarchical Select config form.
    $parents = array(
      'hierarchical_select',
    );
    $form['#submit']['hierarchical_select_common_config_form_submit'] = array(
      $parents,
    );

    // Add a validate callback to override the freetagging and multiple select
    // settings if necessary.
    $form['#validate']['hierarchical_select_taxonomy_form_vocabulary_validate'] = array();
    $form['#submit']['hierarchical_select_taxonomy_form_vocabulary_submit'] = array();

    // The original #submit callback(s) has/have to be executed afterwards.
    $form['#submit'] = array_merge($form['#submit'], $second_part['#submit']);
    $form += $second_part;
  }
  else {
    if (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && is_array($form['taxonomy'])) {
      foreach ($form['taxonomy'] as $vid => $form_item) {

        // Only apply Hierarchical Select if it's enabled for this vocabulary.
        if (is_numeric($vid) && variable_get("taxonomy_hierarchical_select_{$vid}", 0)) {
          require_once drupal_get_path('module', 'hierarchical_select') . '/includes/common.inc';

          // #size is set as soon as save_lineage or the dropbox is enabled,
          // because then "multiple select" is enabled. Unset #size.
          unset($form['taxonomy'][$vid]['#size']);
          $form['taxonomy'][$vid]['#type'] = 'hierarchical_select';
          $form['taxonomy'][$vid]['#config'] = array(
            'module' => 'hs_taxonomy',
            'params' => array(
              'vid' => $vid,
              'exclude_tid' => NULL,
              'root_term' => NULL,
            ),
          );
          hs_taxonomy_hierarchical_select_update_form_item($form['taxonomy'][$vid], $vid);
        }
      }
    }
    else {
      if ($form_id == 'taxonomy_form_term') {
        unset($form['parent']['#options']);
        unset($form['parent']['#theme']);
        $form['parent']['#type'] = 'hierarchical_select';
        $form['parent']['#config'] = array(
          'module' => 'hs_taxonomy',
          'enforce_deepest' => 0,
          'save_lineage' => 0,
          'params' => array(
            'vid' => $form['vid']['#value'],
            'exclude_tid' => $form['tid']['#value'],
            'root_term' => TRUE,
          ),
        );

        // When 'multiple parents' are enabled, we should support that as well!
        if ($form['parent']['#multiple']) {
          unset($form['parent']['#size']);
          $form['parent']['#config']['dropbox']['status'] = 1;
        }
      }
      else {
        if ($form_id == 'forum_form_forum' || $form_id == 'forum_form_container') {
          unset($form['parent'][0]['#options']);
          unset($form['parent'][0]['#theme']);
          unset($form['parent'][0]['#required']);
          $form['parent'][0]['#type'] = 'hierarchical_select';
          $form['parent'][0]['#config'] = array(
            'module' => 'hs_taxonomy',
            'enforce_deepest' => 0,
            'save_lineage' => 0,
            'params' => array(
              'vid' => $form['vid']['#value'],
              'exclude_tid' => $form['tid']['#value'],
              'root_term' => TRUE,
            ),
          );
        }
      }
    }
  }
}