The problem here is really important, regardless of the documentation.
In Drupal 7, the problem has been resolved (and the doc may well be wrong). In D7, hook_form_alter() and hook_form_FORMID_alter() no longer compete in the inappropriate way they did in D6 - they fire in order by weight. So a given module weight will execute the form_alter and the formid_alter at about the same time.
In Drupal 6, you *very* often can't achieve what you want to with hook_form_FORMID_alter() because *all* of the hook_form_alters run after all of the hook_form_FORMID_alters. That means that you *can't* alter a prepared node form successfully with hook_form_FORMID_alter() because all of the things that have built up the form (using hook_form_alter()) haven't run yet. So the form doesn't even look right yet. hook_form_alter() is often the only way around this.
As a general statement, hook_form_FORM_ID_alter() in Drupal 6 is often not useful because of this and you *always* have to be aware of this problem. And in general, this problem is fixed very nicely in Drupal 7. Thanks effulgentsia and sun!
-Randy
I just filed an issue:
http://drupal.org/node/1031458
The doc is only incorrect in D6. In D7 the order has changed.
--Jennifer--
On 1/18/2011 7:37 AM, Justin Ellison wrote:
Code speaks truth, you're correct. However, there's some
contradicting documentation.
>From http://api.drupal.org/api/drupal/modules--system--system.api.php/function/hook_form_alter/7:"For each module (in system weight order) the general form alter hook
implementation is invoked first, then the form ID specific alter
implementation is called."
>From http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_FORM_ID_alter/6:"Note that this hook fires before hook_form_alter(). Therefore all
implementations of hook_form_FORM_ID_alter() will run before all
implementations of hook_form_alter(), regardless of the module order."
Unfortunately, Google returns the former link first, which must've
been what I learned from.
Sorry for any confusion,
Justin
On Tue, Jan 18, 2011 at 9:26 AM, Steve Ringwood<nevets@tds.net> wrote:
Justin
Not sure what you are referring to in the docs for hook_form_alter but
here is the relavent code
from form.inc, function drupal_prepare_form().
$data =&$form;
$data['__drupal_alter_by_ref'] = array(&$form_state);
drupal_alter('form_'. $form_id, $data);<< Form ID specific call
// __drupal_alter_by_ref is unset in the drupal_alter() function, we need
// to repopulate it to ensure both calls get the data.
$data['__drupal_alter_by_ref'] = array(&$form_state);
drupal_alter('form', $data, $form_id);<< General call (hook_form_alter)
Nevets
Jennifer Hodgdon * Poplar ProductivityWare
www.poplarware.com
Drupal web sites and custom Drupal modules