2

I've setup a custom installation profile, and of my installation steps is setting up Google Analytics. I've downloaded the Google Analytics module and enabled it in my site profile.

What I'm doing in my install step is as follows:

hook_install_tasks()

$tasks['site_google_analytics_form'] = array(
  'display' => TRUE,
  'type' => 'form',
  'display_name' => st('Google Analytics Configuration'),
);

site_google_analytics_form()

/**
 * Configure google analytics.  The only required field is the tracking code.
 */
function site_google_analytics_form() {
  module_load_include('inc', 'googleanalytics', 'googleanalytics.admin');
  return drupal_get_form('googleanalytics_admin_settings_form');
}

The probelm I'm having is that, even if I enter a valid web property ID, the form always comes back saying that an invalid ID was entered. I checked to make sure the validation function is firing (which also is in googleanalytics.admin.inc), and it is, but the $form_state variable doesn't have any of the form values preserved.

So, I guess my question is this - why is the $form_state variable being passed into the validation function not storing the values I specified in the form during the install step?

6

The problem

A bad implementation of your code is causing the confusion. The input data is not stored in the $form_state variable, but in $form_state['input'] initially.

Once the user submit a form, Drupal will build the form again and process the values. In this step, the system make some checks. You can take a look of functions that have been called.

enter image description here

The function _form_builder_handle_input_element($form_id, &$element, &$form_state) is the one that set the values in $form.

The previous function is called in the middle of form_builder(). At the end of same function, drupal will fill the values in '$form_state' variable .

Why the function is not filling the values?

Because the next code is checking the input form:

// Set a flag if we have a correct form submission. This is always TRUE for
// programmed forms coming from drupal_form_submit(), or if the form_id coming
// from the POST data is set and matches the current form_id.

if ($form_state['programmed'] || (!empty($form_state['input']) && (isset($form_state['input']['form_id']) && ($form_state['input']['form_id'] == $form_id)))) {
  $form_state['process_input'] = TRUE;
}
else {
  $form_state['process_input'] = FALSE;
}

The result is FALSE because $form_state['input']['form_id'] != $form_id.

'site_google_analytics_form' != 'googleanalytics_admin_settings_form'

So, the input finally is not processed for security reasons

Solution

Your code should be implemented like this:

/*
* hook_install_tasks(&$install_state)
*/
function hook_install_tasks(&$install_state){

$tasks['site_google_analytics_form'] = array(
        'display' => TRUE,
        'type' => 'form',
        'display_name' => st('Google Analytics Configuration'),
);

return $tasks;

}

/**
* Configure google analytics.  The only required field is the tracking code.
*/
function site_google_analytics_form($form, &$form_state){

    module_load_include('inc', 'googleanalytics', 'googleanalytics.admin');
    return googleanalytics_admin_settings_form($form, $form_state);

}

The run_task() call itself the drupal_get_form(), so it was no necessary call yourself in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.