[development] a "form_initialize" function? (was "converting a module from Drupal 5 to Drupal 6")

Sheryl (Permutations Software) sheryl at permutations.com
Wed Jan 21 22:12:11 UTC 2009


I figured out what the module code was trying to do by creating the error.
It's actually not well implemented even in Drupal 5, but I don't know if
there is a function in Drupal to handle the case.

When the administration form for the module is first loaded, the code checks
to see if the module has been correctly installed. If it hasn't, it displays
an error message at the top of the form. It attributes the error to the
submit button just as a convenience. But the user still can see and submit
the form. Ideally, there would be a form initialization function that
checked that all was well before the form was even displayed. If all was not
well, the user would see the error message, but no admin form.

I don't see a _form_initialize function anywhere in Drupal. Is it there
somewhere and I missed it? If not, it might be a good thing to add.

In the meantime, how do I get the name of the submit button so I can use the
same technique the module is currently using, but rewritten for Drupal 6?
The button is not explicitly defined anywhere in the code - it must be using
defaults. Also, according to the Drupal 6 docs, the name for buttons
assigned by the system is 'op' - suggesting that the existing code is
already correct:

http://api.drupal.org/api/file/developer/topics/forms_api_reference.html

The following is a list of default values which do not need to be set (found
in system_elements):

button 
#name = 'op' 
#button_type = 'submit' 
#executes_submit_callback = FALSE 
#ahah['event'] = 'click'
-------------------------------------

Thus this statement is exactly correct for what I want to do, even in Drupal
6:

form_set_error('op', 'error text');

Am I missing something here?



-----Original Message-----
From: development-bounces at drupal.org [mailto:development-bounces at drupal.org]
On Behalf Of Jakob Petsovits
Sent: Wednesday, January 21, 2009 2:09 PM
To: development at drupal.org
Subject: Re: [development] converting a module from Drupal 5 to Drupal 6

On Wednesday, 21. January 2009, Steven Jones wrote:
> Use form_set_error
> (http://api.drupal.org/api/function/form_set_error/6) and call it with
> the first parameter as 'NULL', then the error will be set on the form
> as a whole, not an individual element.

form_set_error() encloses the error assignment in this if-condition:

if (isset($name) && !isset($form[$name])) {
  (...set the form error...)
}

which means when the first parameter ($name) is NULL then no error will be
set.
It seems you need to set the error for any of your form elements, presumably

the submit button in your case was used because the submit button was the
only 
element that's remotely suitable for that error.

You might use the actual submit button as error target in Drupal 6 (...I
think 
that should work) by doing a form_set_error('mybuttonname', t('message')) or
- 
alternatively, same effect - form_error($form['mybuttonname'],
t('message')).

Cheers,
  Jakob




More information about the development mailing list