View source
<?php
namespace Drupal\brightcove\Form;
use Drupal\brightcove\BrightcoveUtil;
use Drupal\brightcove\Entity\BrightcoveSubscription;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityConfirmFormBase;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Queue\QueueInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BrightcoveAPIClientDeleteForm extends EntityConfirmFormBase {
protected $queryFactory;
protected $connection;
protected $playlistLocalDeleteQueue;
protected $videoLocalDeleteQueue;
protected $playerDeleteQueue;
protected $customFieldDeleteQueue;
protected $textTrackDeleteQueue;
protected $subscriptionDeleteQueue;
public function __construct(QueryFactory $query_factory, Connection $connection, QueueInterface $playlist_local_delete_queue, QueueInterface $video_local_delete_queue, QueueInterface $player_delete_queue, QueueInterface $custom_field_delete_queue, QueueInterface $text_track_delete_queue, QueueInterface $subscription_delete_queue) {
$this->queryFactory = $query_factory;
$this->connection = $connection;
$this->playlistLocalDeleteQueue = $playlist_local_delete_queue;
$this->videoLocalDeleteQueue = $video_local_delete_queue;
$this->playerDeleteQueue = $player_delete_queue;
$this->customFieldDeleteQueue = $custom_field_delete_queue;
$this->textTrackDeleteQueue = $text_track_delete_queue;
$this->subscriptionDeleteQueue = $subscription_delete_queue;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.query'), $container
->get('database'), $container
->get('queue')
->get('brightcove_playlist_local_delete_queue_worker'), $container
->get('queue')
->get('brightcove_video_local_delete_queue_worker'), $container
->get('queue')
->get('brightcove_player_delete_queue_worker'), $container
->get('queue')
->get('brightcove_custom_field_delete_queue_worker'), $container
->get('queue')
->get('brightcove_text_track_delete_queue_worker'), $container
->get('queue')
->get('brightcove_subscription_delete_queue_worker'));
}
public function getQuestion() {
return $this
->t('Are you sure you want to delete %name?', [
'%name' => $this->entity
->label(),
]);
}
public function getDescription() {
return parent::getDescription() . '<br>' . $this
->t('Warning: By deleting API Client all of its local contents will be deleted too, including videos, playlists, players, custom fields and subscriptions.');
}
public function getCancelUrl() {
return new Url('entity.brightcove_api_client.collection');
}
public function getConfirmText() {
return $this
->t('Delete');
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$entity = $this->entity;
foreach (BrightcoveUtil::getStatusQueues() as $queue) {
BrightcoveUtil::clearQueue($queue);
}
$playlists = $this->queryFactory
->get('brightcove_playlist')
->condition('api_client', $entity
->id())
->execute();
foreach ($playlists as $playlist) {
$this->playlistLocalDeleteQueue
->createItem($playlist);
}
$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);
}
$videos = $this->queryFactory
->get('brightcove_video')
->condition('api_client', $entity
->id())
->execute();
foreach ($videos as $video) {
$this->videoLocalDeleteQueue
->createItem($video);
}
$players = $this->queryFactory
->get('brightcove_player')
->condition('api_client', $entity
->id())
->execute();
foreach ($players as $player) {
$this->playerDeleteQueue
->createItem([
'player_entity_id' => $player,
]);
}
$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);
}
$default_subscription = BrightcoveSubscription::loadDefault($entity);
if (!empty($default_subscription)) {
if ($default_subscription
->isActive()) {
$default_subscription
->delete();
}
else {
$default_subscription
->delete(TRUE);
}
}
$brightcove_subscriptions = BrightcoveSubscription::loadMultipleByApiClient($entity);
foreach ($brightcove_subscriptions as $brightcove_subscription) {
$this->subscriptionDeleteQueue
->createItem([
'subscription_id' => $brightcove_subscription
->getBcSid(),
'local_only' => TRUE,
]);
}
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',
],
],
],
]);
$entity
->delete();
drupal_set_message($this
->t('Entity @type: deleted @label.', [
'@type' => $this->entity
->bundle(),
'@label' => $this->entity
->label(),
]));
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}