You are here

public function AppForm::save in Apigee Edge 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/Entity/Form/AppForm.php, line 133

Class

AppForm
Base entity form for developer- and team (company) app create/edit forms.

Namespace

Drupal\apigee_edge\Entity\Form

Code

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

  /** @var \Drupal\apigee_edge\Entity\AppInterface $app */
  $app = $this->entity;
  $was_new = $app
    ->isNew();
  $context = [
    '@app' => $this
      ->appEntityDefinition()
      ->getSingularLabel(),
    '@operation' => $was_new ? $this
      ->t('created') : $this
      ->t('updated'),
  ];

  // First save the app entity on Apigee Edge.
  $result = $this
    ->saveApp();
  if ($result > 0) {

    // Then apply credential changes on the app.
    $credential_save_result = $this
      ->saveAppCredentials($app, $form_state);

    // If credential save result is either successful (TRUE) or NULL
    // (no operation performed because there were no change in API product
    // association) then we consider the app save as successful.
    if ($credential_save_result ?? TRUE) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('@app has been successfully @operation.', $context));

      // Also, if app credential(s) could be successfully saved as well then
      // display an extra confirmation message about this.
      if ($credential_save_result === TRUE) {
        $this
          ->messenger()
          ->addStatus($this
          ->t("Credential's product list has been successfully updated."));
      }

      // Only redirect the user from the add/edit form if all operations
      // could be successfully performed.
      $form_state
        ->setRedirectUrl($this
        ->getRedirectUrl());
    }
    else {

      // Display different error messages on app create/edit.
      if ($was_new) {
        $this
          ->messenger()
          ->addError($this
          ->t('Unable to set up credentials on the created app. Please try again.'));
      }
      else {
        $this
          ->messenger()
          ->addError($this
          ->t('Unable to update credentials on the app. Please try again.'));
      }
    }
  }
  else {
    $this
      ->messenger()
      ->addError($this
      ->t('@app could not be @operation. Please try again.', $context));
  }
  return $result;
}