You are here

public function CclAddForm::submitForm in Custom Contextual Links 8

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 FormInterface::submitForm

File

src/Form/CclAddForm.php, line 252
Contains \Drupal\ccl\Form\CclAddForm.

Class

CclAddForm

Namespace

Drupal\ccl\Form

Code

public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {

  // Clean up the values array.
  $form_state
    ->cleanValues();
  $values = $form_state
    ->getValues();

  // Extract the options out of the values.
  $options = [];
  foreach ($values as $key => $data) {
    if (strpos($key, 'ccl_') === FALSE) {
      $options[$key] = $data;
    }
  }
  $record = [
    'type' => $values['ccl_type'],
    'title' => $values['ccl_title'],
    'link' => $values['ccl_link'],
    'options' => serialize($options),
  ];
  if (!$form_state
    ->get([
    'clid',
  ])) {
    $record['clid'] = $form_state
      ->get([
      'clid',
    ]);
    $res = \Drupal::database()
      ->merge('ccl')
      ->fields($record)
      ->key([
      'clid',
    ])
      ->execute();
  }
  else {
    $res = \Drupal::database()
      ->insert('ccl')
      ->fields($record)
      ->execute();
  }
  if ($res) {
    drupal_set_message(t('Contextual link saved.'));
    _ccl_update_cache();
  }
  else {
    drupal_set_message(t('There was an error writing this record to the database. Please try again.'), 'error');
  }
  drupal_goto('admin/config/user-interface/ccl');
}