You are here

public function BrightcoveAPIClientDeleteForm::submitForm in Brightcove Video Connect 8

Same name and namespace in other branches
  1. 8.2 src/Form/BrightcoveAPIClientDeleteForm.php \Drupal\brightcove\Form\BrightcoveAPIClientDeleteForm::submitForm()
  2. 3.x src/Form/BrightcoveAPIClientDeleteForm.php \Drupal\brightcove\Form\BrightcoveAPIClientDeleteForm::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/BrightcoveAPIClientDeleteForm.php, line 154

Class

BrightcoveAPIClientDeleteForm
Builds the form to delete Brightcove API Client entities.

Namespace

Drupal\brightcove\Form

Code

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

  /** @var \Drupal\brightcove\Entity\BrightcoveAPIClient $entity */
  $entity = $this->entity;

  // Empty queues.
  foreach (BrightcoveUtil::getStatusQueues() as $queue) {
    BrightcoveUtil::clearQueue($queue);
  }

  // Collect all playlists belonging for the api client.
  $playlists = $this->queryFactory
    ->get('brightcove_playlist')
    ->condition('api_client', $entity
    ->id())
    ->execute();
  foreach ($playlists as $playlist) {
    $this->playlistLocalDeleteQueue
      ->createItem($playlist);
  }

  // Collect all text tracks belonging for the api client.
  $query = $this->connection
    ->select('brightcove_text_track', 'btt')
    ->fields('btt', [
    'text_track_id',
  ]);
  $query
    ->innerJoin('brightcove_video__text_tracks', 'bvtt', '%alias.text_tracks_target_id = btt.bcttid');
  $query
    ->innerJoin('brightcove_video', 'bv', '%alias.bcvid = bvtt.entity_id');
  $text_tracks = $query
    ->condition('api_client', $entity
    ->id())
    ->execute();
  foreach ($text_tracks as $text_track) {
    $this->textTrackDeleteQueue
      ->createItem($text_track->text_track_id);
  }

  // Collect all videos belonging for the api client.
  $videos = $this->queryFactory
    ->get('brightcove_video')
    ->condition('api_client', $entity
    ->id())
    ->execute();
  foreach ($videos as $video) {
    $this->videoLocalDeleteQueue
      ->createItem($video);
  }

  // Collect all players belonging for the api client.
  $players = $this->queryFactory
    ->get('brightcove_player')
    ->condition('api_client', $entity
    ->id())
    ->execute();
  foreach ($players as $player) {
    $this->playerDeleteQueue
      ->createItem([
      'player_entity_id' => $player,
    ]);
  }

  // Collect all custom fields belonging for the api client.
  $custom_fields = $this->queryFactory
    ->get('brightcove_custom_field')
    ->condition('api_client', $entity
    ->id())
    ->execute();
  foreach ($custom_fields as $custom_field) {
    $this->customFieldDeleteQueue
      ->createItem($custom_field);
  }

  // First delete the default subscription from Brightcove if it's active.
  $default_subscription = BrightcoveSubscription::loadDefault($entity);
  if (!empty($default_subscription)) {
    if ($default_subscription
      ->isActive()) {
      $default_subscription
        ->delete();
    }
    else {
      $default_subscription
        ->delete(TRUE);
    }
  }

  // Then collect all available subscriptions belonging to the given API
  // client, and put them into the delete queue.
  $brightcove_subscriptions = BrightcoveSubscription::loadMultipleByApiClient($entity);
  foreach ($brightcove_subscriptions as $brightcove_subscription) {
    $this->subscriptionDeleteQueue
      ->createItem([
      'subscription_id' => $brightcove_subscription
        ->getBcSid(),
      'local_only' => TRUE,
    ]);
  }

  // Initialize batch.
  batch_set([
    'operations' => [
      [
        [
          BrightcoveUtil::class,
          'runQueue',
        ],
        [
          'brightcove_playlist_local_delete_queue_worker',
        ],
      ],
      [
        [
          BrightcoveUtil::class,
          'runQueue',
        ],
        [
          'brightcove_video_local_delete_queue_worker',
        ],
      ],
      [
        [
          BrightcoveUtil::class,
          'runQueue',
        ],
        [
          'brightcove_player_delete_queue_worker',
        ],
      ],
      [
        [
          BrightcoveUtil::class,
          'runQueue',
        ],
        [
          'brightcove_custom_field_delete_queue_worker',
        ],
      ],
      [
        [
          BrightcoveUtil::class,
          'runQueue',
        ],
        [
          'brightcove_text_track_delete_queue_worker',
        ],
      ],
      [
        [
          BrightcoveUtil::class,
          'runQueue',
        ],
        [
          'brightcove_subscription_delete_queue_worker',
        ],
      ],
    ],
  ]);

  // Delete api client.
  $entity
    ->delete();
  drupal_set_message($this
    ->t('Entity @type: deleted @label.', [
    '@type' => $this->entity
      ->bundle(),
    '@label' => $this->entity
      ->label(),
  ]));
  $form_state
    ->setRedirectUrl($this
    ->getCancelUrl());
}