public function GConfigForm::submitForm in Jammer 1.0.x
Form submission handler.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides ConfigFormBase::submitForm
File
- src/
Form/ GConfigForm.php, line 446
Class
Namespace
Drupal\jammer\FormCode
public function submitForm(array &$form, FormStateInterface $form_state) {
$messenger = \Drupal::messenger();
// get calling parameters
$action = \Drupal::routeMatch()
->getParameter('action');
$id = \Drupal::routeMatch()
->getParameter('id');
// get our array of data
$config = $this
->config('jammer.settings');
$tmp = $config
->get('stored_values');
$data = unserialize($tmp);
// bad action value (TODO: improve routing to prevent this to happen)
if (isset($action) and $action != 'delete' and $action != 'modify' and $action != 'import' and $action != 'export' and $action != 'clear') {
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Bad action type'), $messenger::TYPE_ERROR);
return;
}
// should not occur
if (isset($action) and $action == 'export') {
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Bad action type (\'export\' is not accessible to \'submit\')'), $messenger::TYPE_ERROR);
return;
}
// request for clear
if (isset($action) and $action == 'clear') {
$data = [];
$config
->set('stored_values', serialize($data));
$config
->save();
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Configuration all cleared'), $messenger::TYPE_STATUS);
return;
}
// request for import
if (isset($action) and $action == 'import') {
// get data to import
$import = trim($form_state
->getValue('import'));
// make some sanity checks on imported data
$tmp = @unserialize($import);
$ok = TRUE;
if ($tmp === FALSE) {
$ok = FALSE;
$msg = $this
->t('corrupted data');
}
if (!is_array($tmp)) {
$ok = FALSE;
$msg = $this
->t('bad format');
}
// note: should add more sanity checks (data structure)
if (!$ok) {
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Error with import-data: ') . $msg, $messenger::TYPE_ERROR);
return;
}
// record it into configuration
$config
->set('stored_values', $import);
$config
->save();
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Configuration imported'), $messenger::TYPE_STATUS);
return;
}
// request for deleting entry
if (isset($action) and $action == 'delete') {
// bad $id
if (!isset($id) or !isset($data[$id])) {
$messenger
->addMessage($this
->t('Bad or missing \'id\' for \'delete\' action'), $messenger::TYPE_ERROR);
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
return;
}
// unset line and rebuild array
unset($data[$id]);
$data = array_values($data);
// save new configuration
$config
->set('stored_values', serialize($data));
$config
->save();
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
// this is to prevent the default submit message
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Entry deleted'), $messenger::TYPE_STATUS);
return;
}
// request for modifying entry
if (isset($action) and $action == 'modify') {
// bad $id
if (!isset($id) or !isset($data[$id])) {
$messenger
->addMessage($this
->t('Bad or missing \'id\' for \'modify\' action'), $messenger::TYPE_ERROR);
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
return;
}
// prepare new values for entry
$entry = $this
->buildLine($form_state);
// modify entry
$data[$id] = $entry;
// save new configuration
$config
->set('stored_values', serialize($data));
$config
->save();
// go back to main config page
$form_state
->setRedirect('jammer.jammer_config');
parent::submitForm($form, $form_state);
// this is to prevent the default submit message
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Entry modified'), $messenger::TYPE_STATUS);
return;
}
// no action: request for creating entry
$entry = $this
->buildLine($form_state);
// check if still exists an entry with same form and field(s) and generate a warning
$warn = $this
->entryMatch($entry, $data);
// add entry
$data[] = $entry;
// save new configuration
$config
->set('stored_values', serialize($data));
$config
->save();
parent::submitForm($form, $form_state);
// this is to prevent the default submit message
$messenger
->deleteByType($messenger::TYPE_STATUS);
$messenger
->addMessage($this
->t('Entry added'), $messenger::TYPE_STATUS);
if ($warn) {
$messenger
->addMessage($this
->t('An entry still has this form and field(s). You should check for duplicated/conflicting entries'), $messenger::TYPE_WARNING);
}
return;
}