1

I'm using hook_node_presave to pre-populate taxonomy field with group's audience value. Thus, I'm trying to hide taxonomy field on a node data entry form. I tried hook_form_alter, but it didn't work for me. Is it possible to hide it?

Vlad Vinnikov
  • 1,457
  • 3
  • 22
  • 33

1 Answers1

5
<?php

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'contenttype_node_form') {
    unset($form['somefield']);
  }
}
?>

This works for me just fine. Just change the form id and key of the form field you are targeting, and the field should go away.

Another option would be hiding it with CSS if the input was overridden in presave anyway.

Coder1
  • 13,139
  • 15
  • 59
  • 89
  • I went the css route as form_alter didn't work for me. I tried the following: `function test_prepopulate_form_alter(&$form, &$form_state, $form_id) { watchdog("test_prepopulate", 'field_test %field_test', array('%field_test' => print_r($form, true))); }` I added/edited node, but I didn't see a message in a log. Am I doing something wrong? – Vlad Vinnikov Jul 26 '11 at 17:58
  • That function look pretty wrong as you're not checking for a form id... however, it should be writing to the db if any form is loaded. Is it not getting hit? Try flushing the drupal cache and try it again. It should only work if your module is in fact named 'test_prepopulate'. – Coder1 Jul 26 '11 at 18:07
  • 1
    @Vlad Vinnikau: if you're trying to use the [`hook_form_FORM_ID_alter`](http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_FORM_ID_alter/7) naming convention then it needs to be `yourmodulename_form_test_prepopulate_form_alter(...)` or something like that. Check to make sure your function is named in the correct format. – nmc Jul 26 '11 at 18:14