You are here

public function BrightcoveSubscriptionForm::submitForm in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/Form/BrightcoveSubscriptionForm.php \Drupal\brightcove\Form\BrightcoveSubscriptionForm::submitForm()
  2. 3.x src/Form/BrightcoveSubscriptionForm.php \Drupal\brightcove\Form\BrightcoveSubscriptionForm::submitForm()

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/BrightcoveSubscriptionForm.php, line 89

Class

BrightcoveSubscriptionForm
Builds the form for Brightcove Subscription add, edit.

Namespace

Drupal\brightcove\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  try {
    $brightcove_subscription = BrightcoveSubscription::createFromArray([
      'api_client_id' => $form_state
        ->getValue('api_client_id'),
      'endpoint' => $form_state
        ->getValue('endpoint'),
      'events' => array_values($form_state
        ->getValue('events')),
    ]);
    $brightcove_subscription
      ->save(TRUE);
    drupal_set_message($this
      ->t('Created Brightcove Subscription with %endpoint endpoint.', [
      '%endpoint' => $brightcove_subscription
        ->getEndpoint(),
    ]));
  } catch (\Exception $e) {

    // In case of an exception, show an error message and rebuild the form.
    if ($e
      ->getMessage()) {
      drupal_set_message($this
        ->t('Failed to create subscription: %error', [
        '%error' => $e
          ->getMessage(),
      ]), 'error');
    }
    else {
      drupal_set_message($this
        ->t('Failed to create subscription.'), 'error');
    }
    $form_state
      ->setRebuild(TRUE);
  }

  // Redirect back to the Subscriptions list.
  $form_state
    ->setRedirect('entity.brightcove_subscription.list');
}