You are here

function content_taxonomy_form_alter in Content Taxonomy 6

Same name and namespace in other branches
  1. 5 content_taxonomy.module \content_taxonomy_form_alter()
  2. 6.2 content_taxonomy.module \content_taxonomy_form_alter()

Implementation of hook_form_alter().

hides the taxonomy form if there exists a content taxonomy field, which is set to 'hide default taxonomy fields'

File

./content_taxonomy.module, line 335
Defines a field type for referencing a taxonomy term.

Code

function content_taxonomy_form_alter(&$form, $form_state, $form_id) {
  if (isset($form['type']['#value']) && $form['type']['#value'] . '_node_form' == $form_id) {
    if (!isset($form['taxonomy']) || !is_array($form['taxonomy'])) {

      // empty taxonomy arrays can cause errors on preview
      if (isset($form_state['post']['op']) && $form_state['post']['op'] == t('Preview')) {
        $form['taxonomy'] = array(
          '#tree' => TRUE,
        );
        $form['taxonomy']['key'] = array(
          '#type' => 'value',
          '#value' => '',
        );
        return;
      }
    }
    $_content_taxonomy_vids = array();
    $info = _content_type_info();
    $content_type = $info['content types'][$form['type']['#value']];
    foreach ($content_type['fields'] as $field) {
      if ($field['type'] == 'content_taxonomy' && !in_array($field['vid'], $_content_taxonomy_vids)) {
        $_content_taxonomy_vids[] = $field['vid'];
      }
    }
    _content_taxonomy_taxonomy_unset($form['taxonomy'], $_content_taxonomy_vids);

    //hide empty 'Vocabularies' fieldsets
    $empty_fieldset = TRUE;
    if (is_array($form['taxonomy'])) {
      foreach ($form['taxonomy'] as $key => $value) {
        if (is_array($value) && !empty($value)) {
          $empty_fieldset = FALSE;
          break;
        }
      }
    }
    if ($empty_fieldset) {

      // creating an empty taxonomy array is causing errors on preview in the taxonomy module
      // so we create an empty value field as placeholder, which is going to prevent the errors
      $form['taxonomy'] = array(
        '#tree' => TRUE,
      );
      $form['taxonomy']['key'] = array(
        '#type' => 'value',
        '#value' => '',
      );
    }
  }
}