0

Folks, I need to alter the site information form. I used api_form_system_site_information_settings_alter hook to do so. After submitting I need to print a custom message. I used one submission handler, but the data not saved. My .module file is given below.

use Drupal\Core\Form\FormStateInterface;

function api_key_form_system_site_information_settings_alter(&$form, 
 FormStateInterface $form_state, $form_id) {
 if($form_id != 'system_site_information_settings') {
    return;
}
$site_api = \Drupal::config('api_key.settings')->get('siteapikey');

$form['api_settings'] = array(
    '#type' => 'details',
    '#title' =>t('Site API settings'),
    '#open' => true
);

$form['api_settings']['siteapikey'] = [
    '#type' => 'textfield',
    '#title' => t('API Key'),
    '#default_value' => $site_api,
    '#description' => t('Enter the API key for making the site 
        protected on API calls'),
    '#maxlength' => 32,
    '#required' => true,
    '#placeholder'=> t('No API key yet')
];

$value = !empty($site_api)? t('Update Configuration'): t('Save Configuration');
$form['api_settings']['actions']['#type'] = 'actions';
$form['api_settings']['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $value,
    '#button_type' => 'primary',
];
$form['actions']['submit']['#submit'][] = 'api_key_form_submit';
return $form;
 }

function api_key_form_submit(array $form, FormStateInterface $form_state) {

$site_api = \Drupal::config('api_key.settings')->get('siteapikey');
//Reads the new value form the form instance.
$new_value = $form_state->getValue('siteapikey');
//Changes the success message based on the condtions.
if(!empty($site_api)) {
        //Sets update message.
    $message = t('Site API key is changed from @old_value to 
        @new_value', [
            '@old_value'=> $site_api, 
            '@new_value'=> $new_value
        ]);
} else {
        //Sets success message.
    $message = t('@new_value is set as site API key.',
        ['@new_value'=>$new_value]);
}

//Prints the message.
drupal_set_message($message,$type='status', $repeat=true);
}
0

It seems you have success with displaying the message but not with saving the value? For this you need to load the editable version of the config, set the value and save:

function api_key_form_submit($form, FormStateInterface $form_state) {
  $config = \Drupal::service('config.factory')->getEditable('api_key.settings');
  $config->set('siteapikey', $form_state->getValue('siteapikey'));
  $config->save();
}

By the way it's also a good idea to get the value from the editable version because the config might be overridden and you want to fill the form with the original value:

$site_api = \Drupal::service('config.factory')->getEditable('api_key.settings')->get('siteapikey');
  • Thank you for your help. But, still the other form values did not save – Vishnu Jayan Oct 15 '18 at 6:41
  • You overwrite the entire submit button $form['actions']['submit'] including the existing submit handler. Modify only the button text value instead. – 4k4 Oct 15 '18 at 7:00
  • How I can include both my custom submit handler and default handler in the same submit form? – Vishnu Jayan Oct 15 '18 at 9:27
  • With $form['actions']['submit']['#submit'][], as you already have in your code. The problem is that you overwrite this entire array in the code line before as mentioned in the last comment. – 4k4 Oct 15 '18 at 11:10
  • Okay, thank you @4k4. I have figured it out. Thanks for your support. – Vishnu Jayan Oct 15 '18 at 11:14
0

@vishnu jayan You need to change custom submit handler assign.

function api_key_form_system_site_information_settings_alter(&$form, 
 FormStateInterface $form_state, $form_id) {
  if($form_id != 'system_site_information_settings') {
    return;
  }
$site_api = \Drupal::config('api_key.settings')->get('siteapikey');

$form['api_settings'] = array(
    '#type' => 'details',
    '#title' =>t('Site API settings'),
    '#open' => true
);

$form['api_settings']['siteapikey'] = [
    '#type' => 'textfield',
    '#title' => t('API Key'),
    '#default_value' => $site_api,
    '#description' => t('Enter the API key for making the site 
        protected on API calls'),
    '#maxlength' => 32,
    '#required' => true,
    '#placeholder'=> t('No API key yet')
];

  $value = !empty($site_api)? t('Update Configuration'): t('Save Configuration');
  $form['api_settings']['actions']['#type'] = 'actions';
  $form['api_settings']['actions']['#type'] = 'actions';

      $form['#submit'][] = 'api_key_form_submit';
      return $form;
    }

    function api_key_form_submit($form, FormStateInterface $form_state) {
      $config = \Drupal::service('config.factory')->getEditable('api_key.settings');
      $config->set('siteapikey', $form_state->getValue('siteapikey'));
      $config->save();
      //Prints the message.
      drupal_set_message($message,$type='status', $repeat=true);
    }

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.