I've a cck node type called "eklenti". This node type is associated with a vocabulary but i don't want to display taxonomy selection on the node creation form (instead i use a modified NAT module which creates a term with the same name as the node and assigns it to the node automatically when the node is saved).

I searhed in the forums and thought that this could be done with the form alter. I wrote the following as a custom module and enabled it. Now i see the drupal message when i go to create an "eklenti" node type, but the taxonomy selection is still there. I don't know what i did wrong. I'd appreciate any help to make this work.

<?php
  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function hidetax_form_alter(&$form, &$form_state, $form_id) {
  // Only alter node forms
    if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && !isset($form_state['node_preview'])) {
      $node = $form['#node'];
      if($node->type == 'eklenti') {
        $form['taxonomy']['2']['#type'] = 'hidden';
        drupal_set_message("Altering '$form_id'");
      }
    }
  }
?>

Comments

WorldFallz’s picture

perhaps looking at http://drupal.org/project/taxonomy_hide might help.

===
"Give a man a fish and you feed him for a day.
Teach a man to fish and you feed him for a lifetime."
-- Lao Tzu
"God helps those who help themselves." -- Benjamin Franklin
"Search is your best friend." -- Worldfallz

drupaloSa’s picture

Thanks, but after looking at the taxonomy_hide code i couldn't find a solution for my problem. Taxonomy hide uses the following code to hide terms on node display:

/**
 * Helper function to taxonomy_hide_nodeapi() when its time
 * to display the node
 */
function _taxonomy_hide_nodeapi(&$node, $op, $arg = 0, $arg2 = 0) {
  // This module only cares with nodes that have taxonomy terms
  // assigned to them.
  if (!empty($node->taxonomy)) {

    // Get all hidden vocabularies; keys of $hidden are the vocabulary ids.
    // Apply either the node type filter or the global filter, not both
    // First, apply filter for current node type, if it exists, else apply global filter
    // In this way, the setting for the node type overrides the global setting
    // If a filter for this type is not set, the global filter will be used.
    if ($hidden = array_filter(variable_get('taxonomy_hide_vocabularies_'. $node->type, array())) or
        $hidden = array_filter(variable_get('taxonomy_hide_vocabularies', array()))) {

      // Hide terms by removing them from the taxonomy field
      foreach ($node->taxonomy as $key => $value) {
        if (isset($hidden[$value->vid])) {
          unset($node->taxonomy[$key]);
        }
      }
    }

    // Sort terms by sorting the taxonomy field
    if (!empty($node->taxonomy) and variable_get('taxonomy_hide_group_by_vocabulary', 0)) {
      usort($node->taxonomy, "_taxonomy_hide_sort");
    }
  }
}

The terms of specified vocabularies are unset so they are not displayed on the node view. However, i need to remove the term selection from the node creation and editing pages.

diseño web palma de Mallorca’s picture

+1 need this too...

jtherkel’s picture

+1 I'm new to Drupal module development, and I'm seeing the same issue. I can easily hard code the title. Now I want to do the same for a CCK field in Drupal 7.

// This works
$form['title']['#value'] = t('Just testing');

I'm using print_r($form); to get an idea of what's available, and here's a snippet.

[#after_build] => Array ( [0] => field_form_element_after_build )[#field_name] => field_photo_wall_image [#language] => und ) ) [field_tags] => Array ( [#type] => container [#attributes] => Array ( [class] => Array ( [0] => field-type-taxonomy-term-reference [1] => field-name-field-tags [2] => field-widget-options-buttons ) ) [#weight] => -2 [#tree] => 1 [#language] => und [und] => Array ( [#entity_type] => node [#bundle] => photo_wall_photo [#field_name] => field_tags [#language] => und [#columns] => Array ( [0] => tid ) [#title] => photo_wall_term_reference [#description] => To which photo wall should this photo be posted? [#required] => [#delta] => 0 [#type] => radios [#default_value] => [#options] => Array ( [_none] => N/A [2] => foobarphotowall2 [1] => foobarphotowall1 ) [#value_key] => tid [#element_validate] => Array ( [0] => options_field_widget_validate ) [#properties] => Array ( [filter_xss] => 1 [empty_option] => option_none [strip_tags] => [optgroups] => ) 
r.aubin’s picture

Pertains to D6:
Not sure if anyone is still looking for this, but altering the taxonomy #type alone won't get the form element to be hidden. Unset the taxonomy theme function and you'll be able to hide it.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'mytype_node_form':
      $form['taxonomy']['1']['#type'] = 'hidden';
      $form['taxonomy']['1']['#value'] = 4;
      unset($form['taxonomy']['1']['#theme']);
      break;
  }
}

Keep in mind that passing at as a hidden type isn't foolproof. You might be better off using #type='value'. Hope someone finds this helpful!

blairski’s picture

If the taxonomy is set to required in the vocab edit page, you will also need to change that to be not required:

$form['taxonomy'][1]['#required'] = 0;
ramdhanh’s picture

<?php
  /**
   * Implementation of hook_form_alter().
   *
   * The function is named modulename_form_alter.
   */
  function hidetax_form_alter(&$form, &$form_state, $form_id) {
  // Only alter node forms
    if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id && !isset($form_state['node_preview'])) {
      $node = $form['#node'];
      if($node->type == 'eklenti') {
        $form['taxonomy']['2']['#type'] = 'value';
        drupal_set_message("Altering '$form_id'");
      }
    }
  }
?>