The Submission Handler

When drupal_get_form() is called this time, it retrieves the cached form and hands it off to the submission handler. Following the Forms API callback procedure, our handler is called philosopherbios_theme_form_submit().

Here is the function:

* Form submission callback. */

function philosopherbios_theme_form_submit($form, &$form_state) { // Save our state:

variable_set('philosopherbios_theme', TRUE); if ($form_state['values']['choose_theme'] == 0) { // Enable Descartes theme

."WHERE type = 'theme' AND name = 'descartes'"; db_query($sql); // Initialize theme system: system_theme_data();

system_initialize_theme_blocks('descartes'); // Set theme as default variable_set('theme_default', 'descartes');

// Otherwise, leave it at Garland.

The first thing this submission handler does is store a variable in the database:

variable_set('philosopherbios_theme', TRUE);

Here, we use the database to store a token that indicates our state. With this variable stored, we can later (from the profile tasks function) make sure that the form was actually submitted.

I rlTX Such state-related installer variables should always be removed from the I

I database when the installation profile is complete. We will do this shortly. I

Once we have set this state token, we move on to the form.

We gave the user a choice of two themes. Glancing back at the form, the two themes were Descartes and Garland, each of which was given a specific index in an array:

$form['choose_theme'] = array( '#type' => 'radios', '#title' => st('Default Theme'), '#default_value' => 0,

'#options' => array(0 => st('Descartes'), 1 => st('Garland')),

'#description' => st('Set the default site theme.'), '#weight' => 0,

So when we get the form back, we will check to see whether the value of the radios was set to 0 (Descartes) or 1 (Garland).

Actually, to be more precise, we only care if it is 0. If the user chooses the Garland theme, which is already configured and set as the default, then we don't need to do anything. There is work if the user chooses to set Descartes as the default. Thus, we end up with a conditional like this:

if ($form_state['values,][,choose_theme'] == 0) { // Enable Descartes theme $sql = "UPDATE {system} SET status = 1 "

."WHERE type = 'theme' and name = 'descartes'"; db_query($sql); // Initialize theme system: system_theme_data();

system_initialize_theme_blocks('descartes'); // Set theme as default variable_set('theme_default','descartes');

If $form_state['values']['choose_theme'] is 0, we have work to do. In fact, we have three specific tasks to perform:

• Enable the theme

• Initialize the theme

First, we need to enable the theme named descartes. Since there is no convenient function that we can use for this, we do it with SQL:

."WHERE type = 'theme' AND name = 'descartes'"; db_query($sql);

Here we modify the {system} table. The system table is a big storage system for tracking theme and module settings. Fortunately, it is rarely the case that module code must directly manipulate this table. Here, however, we need to work on the table directly.

Basically, we need to change the status flag for the theme named descartes. If status is 0, a theme is disabled. We set it to 1 to enable the theme.

Now that the theme is enabled, we need to initialize the theme.

By default, a theme has no blocks set. But this isn't good. An administrator would not have immediate access to the main menu. Instead, they'd get a screen that looked something like the following screenshot:

^^ Philosopher Bios

Welcome to your new Drupal website!

Please follow these steps to set up and start using your website:

1. Configure your website

Once logged in, visit the administration section, where you can customize and configure all aspects of your website.

2. Enable additional functionality

Next, visit the module list and enable features which suit your specific needs. You can find additional modules in the Drupal modules download section.

3. Customize your website design

To change the "look and feel" of your website, visit the themes section. You may choose from one of the included themes or download additional themes from the Drupal themes download section.

4. Start posting content

Finally, you can create content for your website. This message will disappear once you have promoted a post to the front page.

For more information, please refer to the help section, or the online Drupal handbooks. You may also post at the Drupal forum, or view the wide range of other support options available.

Notice the missing menu on the left side?

Usually, the theme selection system in the Administer | Site building | Themes page handles initializing a new theme and adding the default menu. We need to do that initialization here in the installer, though.

Drupal makes this work easier. It has a built-in method for setting up a theme the first time. But first we need to initialize the data for themes since the installer hasn't already done this step. So we make two function calls:



The first initializes the theme data (basically processing the theme .info files). The second function does the necessary theme initialization, including adding the standard menu to the left-hand column of blocks.

Two steps are done. The third step is to set descartes as the default theme.

Drupal determines which theme is default by using the theme_default variable stored in the main variable table. And as we saw earlier in this chapter and in the last chapter, the contents of the variable table can be manipulated with variable_set(), variable_get(), and variable_del() .

So setting descartes to the default is as easy as this:


Now we have enabled and initialized the theme, and set it as default. When the user makes his or her way through the installer, the first screen he or she will see will look like the following screenshot:

But before we jump that far, we need to work a little on our installer. We need to return to the profile tasks function, philosopherbios_profile_tasks(), to finish up.

Was this article helpful?

0 0

Post a comment