You are here

public function WebhookConfigForm::save in Webhooks 8

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/Form/WebhookConfigForm.php, line 287

Class

WebhookConfigForm
Class Webhook Config Form.

Namespace

Drupal\webhooks\Form

Code

public function save(array $form, FormStateInterface $form_state) {

  /** @var \Drupal\webhooks\Entity\WebhookConfig $webhook_config */
  $webhook_config = $this->entity;

  // Keep the old secret if no new one has been given.
  if (empty($form_state
    ->getValue('secret'))) {
    $webhook_config
      ->set('secret', $form['secret']['#default_value']);
  }
  $active = $webhook_config
    ->save();
  switch ($active) {
    case SAVED_NEW:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Created the %label Webhook.', [
        '%label' => $webhook_config
          ->label(),
      ]));
      break;
    default:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Saved the %label Webhook.', [
        '%label' => $webhook_config
          ->label(),
      ]));
  }

  /** @var \Drupal\Core\Url $url */
  $url = $webhook_config
    ->toUrl('collection');
  $form_state
    ->setRedirectUrl($url);
}