0

In my installation profile, I have added a text field to the install_configure_form by including:

if (!function_exists("system_form_install_configure_form_alter")) {
  function system_form_install_configure_form_alter(&$form, $form_state) {
    $form['site_information']['site_name']['#default_value'] = st('Organization name');
    $form['site_information']['org_short_name'] = array(
      '#type' => 'textfield',
        '#title' => st('Short name'),
        '#description' => st('This will be used as a casual reference to the association throughout the site. If left blank, the Site name will be used.'),
      '#attributes' => ['placeholder' => 'e.g. FBI'],
        '#size' => 60,
        '#maxlength' => 60,
    );
    // Move the email to the bottom
    $form['site_information']['site_mail']['#weight'] = 1;
  }
}

Now, I'm trying to read and persist what the user entered using a _submit function:

function my_profile_install_configure_form_submit(&$form, $form_state) {
  $values = $form_state['values'];
  $name = !empty($values['org_short_name']) ? $values['org_short_name'] : $values['site_name'];

  variable_set('short_name', $name);
}

Unfortunately, I don't think the my_profile_install_configure_form_submit function is getting called because the short_name is not getting created. How should I be accessing and persisting the custom elements in that form?

Thanks!

4
  • 1
    Welcome to Drupal Answers! You never wrap a hook with a if statement like that; plus, you don't need to use ystem_form_install_configure_form_alter as hook name.
    – avpaderno
    Commented Jul 23, 2016 at 3:23
  • You also don't explain why you think that my_profile_install_configure_form_submit() would be called in some way. Did you show all the code you are using?
    – avpaderno
    Commented Jul 23, 2016 at 3:24
  • I thought it was a bit odd how the function was wrapped, but that's what was generated by the profile_builder module and I figured I have less experience than the developers of that module, so who am I to question them? 🙂I also found it strange that the hook didn't follow the typical naming convention by replacing "hook" with my profile name. What threw me off was the fact that despite my skepticism about these things, that function was working fine. It's the other one that wasn't doing its job.
    – pvdjay
    Commented Jul 23, 2016 at 13:37
  • It doesn't make sense because system_form_install_configure_form_alter() doesn't exist in Drupal 7. Plus, you don't use the name of another module/profile to implement a hook, if not when your code is implementing a hook for another module/profile, but that is not what the majority of the modules/profiles do.
    – avpaderno
    Commented Jul 23, 2016 at 14:36

1 Answer 1

0

Altering the site configuration form is not different from altering other forms, as done from a module.

If myprofile.profile is used in your profile, its content should be similar to the following one.

  function myprofile_form_install_configure_form_alter(&$form, $form_state) {
    $form['site_information']['site_name']['#default_value'] = st('Organization name');
    $form['site_information']['org_short_name'] = array(
      '#type' => 'textfield',
        '#title' => st('Short name'),
        '#description' => st('This will be used as a casual reference to the association throughout the site. If left blank, the Site name will be used.'),
      '#attributes' => ['placeholder' => 'e.g. FBI'],
        '#size' => 60,
        '#maxlength' => 60,
    );
    // Move the email to the bottom
    $form['site_information']['site_mail']['#weight'] = 1;

    $form['actions']['submit']['#submit'][] = 'myprofile_install_configure_form_submit';
  }

  // Notice which argument is passed by reference.
  function myprofile_install_configure_form_submit($form, &$form_state) {
    $name = !empty($form_state['values']['org_short_name']) ? $form_state['values']['org_short_name'] : $form_state['values']['site_name'];

    variable_set('short_name', $name);
  }

As for variable_set() working in this case, it works, since install_configure_form_submit() (the default submission handler for the site configuration form) uses it.

  variable_set('site_name', $form_state['values']['site_name']);
  variable_set('site_mail', $form_state['values']['site_mail']);
  variable_set('date_default_timezone', $form_state['values']['date_default_timezone']);
5
  • I don't want to complete override the handling of this form. I just want to be able to process my additions. The example you provided wouldn't allow core's install_configure_form_submit() to run and the site would be broken. Would the appropriate solution be to add a call to install_configure_form_submit() at the end of my submission handler?
    – pvdjay
    Commented Jul 23, 2016 at 14:09
  • You don't need to completely override the handling of the form. In fact, all the hook_form_alter() implementations just add their form fields and submission handlers. The code adds a submission handler; it doesn't replace the existing one.
    – avpaderno
    Commented Jul 23, 2016 at 14:16
  • Hmm, this doesn't seem to be what happened when I tried it. It definitely only ran my handler. Because it ran very quickly and the site was broken.
    – pvdjay
    Commented Jul 23, 2016 at 14:19
  • Adding install_configure_form_submit($form, $form_state); to the end of the handler does seem to work.
    – pvdjay
    Commented Jul 23, 2016 at 14:20
  • I would rather try first to use $form['#submit'][] = 'myprofile_install_configure_form_submit'; instead of $form['actions']['submit']['#submit'][] = 'myprofile_install_configure_form_submit';. If that doesn't work, I would try with $form['actions']['submit']['#submit'][] = 'myprofile_install_configure_form_submit'; $form['actions']['submit']['#submit'][] = 'install_configure_form_submit';.
    – avpaderno
    Commented Jul 23, 2016 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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