You are here

class OverviewForm in Queue UI 8.2

Class QueueUIOverviewForm @package Drupal\queue_ui\Form

Hierarchy

Expanded class hierarchy of OverviewForm

1 string reference to 'OverviewForm'
queue_ui.routing.yml in ./queue_ui.routing.yml
queue_ui.routing.yml

File

src/Form/OverviewForm.php, line 25

Namespace

Drupal\queue_ui\Form
View source
class OverviewForm extends FormBase {

  /**
   * The queue factory.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * The tempstore factory.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The Drupal state storage.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The Drupal module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $dbConnection;

  /**
   * @var \Drupal\Core\Queue\QueueWorkerManager
   */
  private $queueWorkerManager;

  /**
   * @var \Drupal\queue_ui\QueueUIManager
   */
  private $queueUIManager;

  /**
   * OverviewForm constructor.
   *
   * @param \Drupal\Core\Queue\QueueFactory $queue_factory
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   * @param \Drupal\Core\Session\AccountInterface $current_user
   * @param \Drupal\Core\State\StateInterface $state
   * @param \Drupal\Core\Extension\ModuleHandler $module_handler
   * @param \Drupal\Core\Queue\QueueWorkerManager $queueWorkerManager
   * @param \Drupal\queue_ui\QueueUIManager $queueUIManager
   * @param \Drupal\Core\Messenger\Messenger $messenger
   */
  public function __construct(QueueFactory $queue_factory, PrivateTempStoreFactory $temp_store_factory, AccountInterface $current_user, StateInterface $state, ModuleHandler $module_handler, QueueWorkerManager $queueWorkerManager, QueueUIManager $queueUIManager, Messenger $messenger) {
    $this->queueFactory = $queue_factory;
    $this->tempStoreFactory = $temp_store_factory;
    $this->currentUser = $current_user;
    $this->state = $state;
    $this->moduleHandler = $module_handler;
    $this->dbConnection = Database::getConnection('default');
    $this->queueWorkerManager = $queueWorkerManager;
    $this->queueUIManager = $queueUIManager;
    $this->messenger = $messenger;
  }

  /**
   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
   * @return static
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('queue'), $container
      ->get('tempstore.private'), $container
      ->get('current_user'), $container
      ->get('state'), $container
      ->get('module_handler'), $container
      ->get('plugin.manager.queue_worker'), $container
      ->get('plugin.manager.queue_ui'), $container
      ->get('messenger'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'queue_ui_overview_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['top'] = [
      'operation' => [
        '#type' => 'select',
        '#title' => $this
          ->t('Action'),
        '#options' => [
          'submitBatch' => $this
            ->t('Batch process'),
          'submitRelease' => $this
            ->t('Remove leases'),
          'submitClear' => $this
            ->t('Clear'),
        ],
      ],
      'actions' => [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'form-actions',
          ],
        ],
        'apply' => [
          '#type' => 'submit',
          '#tableselect' => TRUE,
          '#submit' => [
            '::submitBulkForm',
          ],
          '#value' => $this
            ->t('Apply to selected items'),
        ],
      ],
    ];
    $form['queues'] = [
      '#type' => 'table',
      '#tableselect' => TRUE,
      '#header' => [
        'title' => $this
          ->t('Title'),
        'items' => $this
          ->t('Number of items'),
        'class' => $this
          ->t('Class'),
        'cron' => $this
          ->t('Cron time limit (seconds)'),
        'operations' => $this
          ->t('Operations'),
      ],
      '#empty' => $this
        ->t('No queues defined'),
    ];
    $queue_order_installed = $this->moduleHandler
      ->moduleExists('queue_order');
    if ($queue_order_installed) {

      // Add the dragable options for the form.
      $form['queues']['#tabledrag'] = [
        [
          'action' => 'order',
          'relationship' => 'sibling',
          'group' => 'queue-order-weight',
        ],
      ];

      // Add the weight to the table header.
      $form['queues']['#header']['weight'] = $this
        ->t('Weight');
      $form['weight'] = [
        '#type' => 'value',
      ];
    }

    // Get queues names.
    $queues = $this->queueWorkerManager
      ->getDefinitions();
    foreach ($queues as $name => $queue_definition) {
      $queue = $this->queueFactory
        ->get($name);
      $operations = [];

      // If queue inspection is enabled for this implementation.
      if ($queue_ui = $this->queueUIManager
        ->fromQueueName($name)) {
        $operations['inspect'] = [
          'title' => $this
            ->t('Inspect'),
          'url' => Url::fromRoute('queue_ui.inspect', [
            'queue_name' => $name,
          ]),
        ];
      }
      $row = [
        'title' => [
          '#markup' => (string) $queue_definition['title'],
        ],
        'items' => [
          '#markup' => $queue
            ->numberOfItems(),
        ],
        'class' => [
          '#markup' => $this->queueUIManager
            ->queueClassName($queue),
        ],
        'cron' => [
          '#type' => 'number',
          '#title' => $this
            ->t('Cron Time'),
          '#title_display' => 'hidden',
          '#placeholder' => $this
            ->t('Cron disabled'),
          '#value' => isset($queue_definition['cron']['time']) ? $queue_definition['cron']['time'] : '',
          '#parents' => [],
          '#name' => 'cron[' . $name . ']',
        ],
        'operations' => [
          '#type' => 'dropbutton',
          '#links' => $operations,
        ],
      ];

      // Enable sort if queue_order is enabled.
      if ($queue_order_installed) {
        $weight = isset($queue_definition['weight']) ? $queue_definition['weight'] : 10;
        $row['#attributes'] = [
          'class' => [
            'draggable',
          ],
        ];
        $row['#weight'] = $weight;
        $row['weight'] = [
          '#type' => 'weight',
          '#title' => $this
            ->t('Weight for @title', [
            '@title' => $name,
          ]),
          '#title_display' => 'invisible',
          '#default_value' => $weight,
          '#name' => 'weight[' . $name . ']',
          // Classify the weight element for #tabledrag.
          '#attributes' => [
            'class' => [
              'queue-order-weight',
            ],
          ],
        ];
      }
      $form['queues'][$name] = $row;
    }
    $form['cron'] = [
      '#type' => 'value',
    ];
    $form['botton'] = [
      'actions' => [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'form-actions',
          ],
        ],
        'apply' => [
          '#type' => 'submit',
          '#tableselect' => TRUE,
          '#submit' => [
            '::submitBulkForm',
          ],
          '#value' => $this
            ->t('Apply to selected items'),
        ],
        'save' => [
          '#type' => 'submit',
          '#value' => $this
            ->t('Save changes'),
        ],
      ],
    ];
    return $form;
  }

  /**
   * We need this method, but each button has its own submit handler.
   *
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $form_state
      ->cleanValues();
    foreach ($form_state
      ->getValue('cron') as $name => $time) {
      $this->state
        ->set('queue_ui_cron_' . $name, $time);
    }

    // Only save the weight if the queue_order module is available.
    if ($this->moduleHandler
      ->moduleExists('queue_order')) {
      $order_config = $this
        ->configFactory()
        ->getEditable('queue_order.settings');

      // Save the weight of the defined workers.
      foreach ($form_state
        ->getValue('weight') as $name => $weight) {
        $order_config
          ->set('order.' . $name, (int) $weight);
      }
      $order_config
        ->save();
    }

    // Clear the cached plugin definition so that changes come into effect.
    $this->queueWorkerManager
      ->clearCachedDefinitions();
  }

  /**
   * @param array $form
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  public function submitBulkForm(array &$form, FormStateInterface $form_state) {
    if (in_array($form_state
      ->getValue('operation'), [
      'submitBatch',
      'submitRelease',
      'submitClear',
    ])) {
      $selected_queues = array_filter($form_state
        ->getValue('queues'));
      if (!empty($selected_queues)) {
        $this
          ->{$form_state
          ->getValue('operation')}($form_state, $selected_queues);
      }
    }
  }

  /**
   * Process queue(s) with batch.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param $queues
   */
  public function submitBatch(FormStateInterface $form_state, $queues) {
    $batch = [
      'title' => $this
        ->t('Processing queues'),
      'operations' => [],
      'finished' => [
        '\\Drupal\\queue_ui\\QueueUIBatch',
        'finish',
      ],
    ];
    foreach ($queues as $queue_name) {
      $batch['operations'][] = [
        '\\Drupal\\queue_ui\\QueueUIBatch::step',
        [
          $queue_name,
        ],
      ];
    }
    batch_set($batch);
  }

  /**
   * Option to remove lease timestamps.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param $queues
   */
  public function submitRelease(FormStateInterface $form_state, $queues) {
    foreach ($queues as $queue_name) {

      /** @var QueueUIInterface $queue_ui */
      if ($queue_ui = $this->queueUIManager
        ->fromQueueName($queue_name)) {
        $num_updated = $queue_ui
          ->releaseItems($queue_name);
        $this->messenger
          ->addMessage($this
          ->t('@count lease reset in queue @name', [
          '@count' => $num_updated,
          '@name' => $queue_name,
        ]));
      }
    }
  }

  /**
   * Option to delete queue.
   *
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   * @param $queues
   */
  public function submitClear(FormStateInterface $form_state, $queues) {
    $this->tempStoreFactory
      ->get('queue_ui_clear_queues')
      ->set($this->currentUser
      ->id(), $queues);
    $form_state
      ->setRedirect('queue_ui.confirm_clear_form');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
OverviewForm::$currentUser protected property The current user.
OverviewForm::$dbConnection protected property The database connection.
OverviewForm::$moduleHandler protected property The Drupal module handler.
OverviewForm::$queueFactory protected property The queue factory.
OverviewForm::$queueUIManager private property
OverviewForm::$queueWorkerManager private property
OverviewForm::$state protected property The Drupal state storage.
OverviewForm::$tempStoreFactory protected property The tempstore factory.
OverviewForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
OverviewForm::create public static function Overrides FormBase::create
OverviewForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
OverviewForm::submitBatch public function Process queue(s) with batch.
OverviewForm::submitBulkForm public function
OverviewForm::submitClear public function Option to delete queue.
OverviewForm::submitForm public function We need this method, but each button has its own submit handler. Overrides FormInterface::submitForm
OverviewForm::submitRelease public function Option to remove lease timestamps.
OverviewForm::__construct public function OverviewForm constructor.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.