I have a very simple module. It hooks hook_admin and hook_menu to create a configuration screen at "admin/settings/mymodule" and a couple of pages with their own urls which are added to a navigation menu.

My question is this. What hook do I implement to execute initialisation code which should run _after_ the user has configured the module via its admin page?

Comments

goofus’s picture

Hi,
Please elaborate. What specifically do you need to do? Do you need to store system wide configuration values (i.e across all uses) or configuration values per user?, per user role?, etc.

Neutrino Sunset’s picture

Storing configuration values isn't the issue. My module creates an an admin/settings page that enables it to be configured like any other module, and this configuration is system wide.

The configuation in question though is login details for an external web service. Once the login details for the external web service have been configured the module needs to access the web service, acquire some data, transform and format the data, and then store the formatted output in the DB so that page retrieves can just load the data without having to do any computationally expensive work.

The process of retrieving data from the web service needs to occur after the configuration details have been entered and then thereafter it will occur once per day which I'm anticipating I can have done by the daily maintenance cron job.

I'm just trying to see which hook I can implement to know when the module has been configured.

jaypan’s picture

I'd suggest doing this:

1) On the settings page submission, use the entered data to check if it is properly able to log into the external service.
2) If it is, set a value in the variables table using variable_set(). For example, you could do this:

function mymodule_settings_page_submit($form, &$form_state)
{
  $success = check_credentials(); // where check_credentials() is a function that returns true or false depending on whether your credentials could be used to log into the external server
  if($success)
  {
    variable_set('mymodule_credentials_approved', TRUE);
  }
}

3) Next, you can use hook_cron to log into the external site and grab the data once a day. In your hook_cron, do a check for the variable that you set, and only access the external server if your variable is set to true:

function mymodule_cron()
{
  if(variable_get('mymodule_credentials_approved', FALSE))
  {
    // log into the external site and grab the data. Add a check to make sure this only happens once every 24 hours. You can do this by setting the time of the last external login in the variables table, same as you did above
  }
}

Side point - there isn't any hook_admin() in drupal 6.

Contact me to contract me for D7 -> D10/11 migrations.

Neutrino Sunset’s picture

Thats exactly what I'm looking for.

I got hook_admin from this tutorial page here http://drupal.org/node/206761 on closer inspection I can see I misread it and that the function that generates the admin page can be declared as anything so long as it is referenced correctly in hook_menu.

Thanks for the help.

Neutrino Sunset’s picture

I'm having trouble getting this to work.

The documentation here http://drupal.org/node/751826 in the section 'Submitting Forms' appears to be describing the technique you suggested. But the comment by the_g_bomb 2010-09-08 states that this functionality has been removed.

I've implemented a validate method as described by the documentation and that does work as the documentation describes, but adding a submit method does not. Given that the documentation indicates that these two functions use a similar naming convention this leads me to believe that I have named the submit method correctly but that it just doesn't work.

I've followed the link supplied by the_g_bomb to here http://drupal.org/node/114774#hook_submit which suggests an alternative way of doing this for Drupal 6. But here I see another problem as the documentation here seems to indicate that it is a method for adding a handler for a submit button explicit added via the hook_form. But that is not the case in this instance as the Submit button is not manually added to the form for a system settings page it is instead provided automatically by some underlying implentation (which presumably already attaches it's own handler).

From viewing the HTML source for the rendered setings page I can see that my form does have a Submit button, but it isn't created by me since the source for my form is similar to this:

<?php
function evecorplist_admin()
{
	$form = array();
	$form["mymodule_control1"] = array(
		"#type" => "textfield",
		"#title" => t("Get some text"),
		"#default_value" => variable_get("mymodule_getsometext", ""),
		"#size" => 64,
		"#maxlength" => 64,
		"#description" => t("Description of get some text."),
		"#required" => TRUE
	);
	return system_settings_form($form);
}
?>

I suspect that the Submit button is added in the call to system_settings_form, I also suspect that the handler for that Submit button is also created and hooked up in that method.

Which all leaves me wondering how can I add a submit handler of my own to this form without breaking the base functionality provided by the system_settings_form method.

jaypan’s picture

First, the comment by the poster was referring to hook_submit(). This hook doesn't exist in Drupal 6. What makes it confusing however, is that _submit() functions do exist in Drupal 6. But these submit functions are not hooks. The way they work is that a function definition has a corresponding _submit() function. For example:

Form definition: my_form()
Submit fucntion: my_form_submit()

Form definition: module_form()
Submit function: module_form_submit()

Form definition: module_settings()
Submit function: module_settings_submit()

However if you return system_settings_form() at the end of your form definition, there ISN'T a submit function for that, as all variables on the page are automatically saved to the variables table in the database. But I'm pretty sure you can add your own submit function to run *after* the submit function that system_settings_form() provides. Just add this to the end of your form definition:

$form['#submit'][] = 'my_settings_submit_function';

You can now use this;

 function my_settings_submit_function($form, &$form_state)
{
  // submit function
}

...I think. I'm pretty sure I've done this in the past, though it was a while back so maybe I did it differently. But I think that's how it can be done.

Contact me to contract me for D7 -> D10/11 migrations.

Neutrino Sunset’s picture

That's got it sorted. Thanks again.