You are here

public function SmsGatewayForm::save in SMS Framework 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/SmsGatewayForm.php, line 299

Class

SmsGatewayForm
Form controller for SMS Gateways.

Namespace

Drupal\sms\Form

Code

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

  /** @var \Drupal\sms\Entity\SmsGatewayInterface $sms_gateway */
  $sms_gateway = $this
    ->getEntity();
  $incoming_push_path_original = $sms_gateway
    ->getPushIncomingPath();
  $incoming_push_path = $form_state
    ->getValue([
    'incoming_messages',
    'push_path',
  ]);
  $reports_push_path_original = $sms_gateway
    ->getPushReportPath();
  $reports_push_path = $form_state
    ->getValue([
    'delivery_reports',
    'push_path',
  ]);
  $sms_gateway
    ->setStatus($form_state
    ->getValue('status'))
    ->setPushReportPath($reports_push_path)
    ->setPushIncomingPath($incoming_push_path);
  $saved = $sms_gateway
    ->save();
  if ($saved == SAVED_NEW) {
    drupal_set_message($this
      ->t('Gateway created.'));
    $rebuild = !empty($incoming_push_path) || !empty($reports_push_path);

    // Redirect to edit form.
    $form_state
      ->setRedirectUrl(Url::fromRoute('entity.sms_gateway.edit_form', [
      'sms_gateway' => $sms_gateway
        ->id(),
    ]));
  }
  else {
    drupal_set_message($this
      ->t('Gateway saved.'));

    // Only rebuild routes if the paths changed.
    $rebuild_incoming = $incoming_push_path_original != $incoming_push_path;
    $rebuild_reports = $reports_push_path_original != $reports_push_path;
    $rebuild = $rebuild_incoming || $rebuild_reports;

    // Back to list page.
    $form_state
      ->setRedirect('sms.gateway.list');
  }
  if ($rebuild) {
    $this->routeBuilder
      ->setRebuildNeeded();
  }
}