You are here

public function BrightcoveAPIClientForm::submitForm in Brightcove Video Connect 8

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

This is the default entity object builder function. It is called before any other submit handler to build the new entity object to be used by the following submit handlers. At this point of the form workflow the entity is validated and the form state can be updated, this way the subsequently invoked handlers can retrieve a regular entity object to act on. Generally this method should not be overridden unless the entity requires the same preparation for two actions, see \Drupal\comment\CommentForm for an example with the save and preview actions.

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

File

src/Form/BrightcoveAPIClientForm.php, line 250

Class

BrightcoveAPIClientForm
Class BrightcoveAPIClientForm.

Namespace

Drupal\brightcove\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient $entity */
  $entity = $this->entity;
  $client = new Client($this->keyValueExpirableStore
    ->get($form_state
    ->getValue('id')));
  $cms = new CMS($client, $form_state
    ->getValue('account_id'));

  /** @var \Brightcove\Object\CustomFields $video_fields */
  $video_fields = $cms
    ->getVideoFields();
  $entity
    ->setMaxCustomFields($video_fields
    ->getMaxCustomFields());
  foreach ($video_fields
    ->getCustomFields() as $custom_field) {

    // Create queue item.
    $this->customFieldQueue
      ->createItem([
      'api_client_id' => $this->entity
        ->id(),
      'custom_field' => $custom_field,
    ]);
  }
  parent::submitForm($form, $form_state);
  if ($entity
    ->isNew()) {

    // Get Players the first time when the API client is being saved.
    $pm = new PM($client, $form_state
      ->getValue('account_id'));
    $player_list = $pm
      ->listPlayers();
    $players = [];
    if (!empty($player_list) && !empty($player_list
      ->getItems())) {
      $players = $player_list
        ->getItems();
    }
    foreach ($players as $player) {

      // Create queue item.
      $this->playerQueue
        ->createItem([
        'api_client_id' => $entity
          ->id(),
        'player' => $player,
      ]);
    }

    // Get Custom fields the first time when the API client is being saved.

    /** @var \Brightcove\Object\CustomFields $video_fields */
    $video_fields = $cms
      ->getVideoFields();
    foreach ($video_fields
      ->getCustomFields() as $custom_field) {

      // Create queue item.
      $this->customFieldQueue
        ->createItem([
        'api_client_id' => $entity
          ->id(),
        'custom_field' => $custom_field,
      ]);
    }

    // Create new default subscription.
    $subscription = new BrightcoveSubscription(TRUE);
    $subscription
      ->setStatus(FALSE)
      ->setApiClient($entity)
      ->setEndpoint(BrightcoveUtil::getDefaultSubscriptionUrl())
      ->setEvents([
      'video-change',
    ])
      ->save();

    // Get Subscriptions the first time when the API client is being saved.
    $this->subscriptionsQueue
      ->createItem($entity
      ->id());
  }
}