[development] a "form_initialize" function? (was "converting a module from Drupal 5 to Drupal 6")
Jamie Holly
hovercrafter at earthlink.net
Wed Jan 21 23:58:11 UTC 2009
The simple solution:
Under the _menu hook where you define the settings page
'page callback'=>'mymodule_setttings_page'
Then create that new function
function mymodule_settings_page(){
if (!function_to_test_appropriate_installation()){
return "<p>OOPS you didn't configure your module correctly</p>";
}
return drupal_get_form("mysettings_form_id");
}
Jamie Holly
Chris Johnson wrote:
> There has to be a better solution than a form_initialize() hook. This
> is not criticism directed at you, Sheryl. But I think you've got a
> use case that should be addressed, assuming there is not an existing
> way to do it. I may well be ignorant of the proper, Drupal-ish way of
> handling this situation.
>
> ..chris
>
> On Wed, Jan 21, 2009 at 4:12 PM, Sheryl (Permutations Software)
> <sheryl at permutations.com> wrote:
> > 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