ConfirmClearReplicationQueueForm.php in Workspace 8
File
src/Form/ConfirmClearReplicationQueueForm.php
View source
<?php
namespace Drupal\workspace\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\workspace\Entity\Replication;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ConfirmClearReplicationQueueForm extends ConfirmFormBase {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.manager'));
}
public function getFormId() {
return 'confirm_clear_replication_queue_form';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($this
->clearReplicationQueue()) {
$message = $this
->t('All the queued deployments have been marked as failed and have been removed from the replication queue.');
}
else {
$message = $this
->t('There were not any queued deployments in the replication queue.');
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
$this
->messenger()
->addMessage($message);
}
public function clearReplicationQueue() {
$queued_items_deleted = FALSE;
$queued_replications = $this->entityTypeManager
->getStorage('replication')
->loadByProperties([
'replication_status' => Replication::QUEUED,
]);
foreach ($queued_replications as $replication) {
$replication
->setReplicationStatusFailed()
->setReplicationFailInfo($this
->t('This deployment has been marked as failed manually, when clearing the replication queue.'))
->save();
$queued_items_deleted = TRUE;
}
return $queued_items_deleted;
}
public function getDescription() {
return $this
->t('All replications will be marked as failed and removed from the cron queue, except those that are in progress. This action cannot be undone.');
}
public function getQuestion() {
return $this
->t('Are you sure you want to clear the replication queue?');
}
public function getCancelUrl() {
return new Url('replication.settings_form');
}
public function getConfirmText() {
return $this
->t('Clear queue');
}
}