Accessing values entered in custom fields added to install_configure_form
-
11-12-2020 - |
Question
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!
Solution
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']);