function QueueExampleForm::submitClaimDeleteItem

Same name in other branches
  1. 3.x modules/queue_example/src/Form/QueueExampleForm.php \Drupal\queue_example\Form\QueueExampleForm::submitClaimDeleteItem()
  2. 4.0.x modules/queue_example/src/Form/QueueExampleForm.php \Drupal\queue_example\Form\QueueExampleForm::submitClaimDeleteItem()

Submit function for "Claim and delete" button.

Parameters

array $form: Form definition array.

\Drupal\Core\Form\FormStateInterface $form_state: Form state object.

File

queue_example/src/Forms/QueueExampleForm.php, line 356

Class

QueueExampleForm
Form with examples on how to use queue.

Namespace

Drupal\queue_example\Forms

Code

public function submitClaimDeleteItem(array &$form, FormStateInterface $form_state) {
    $queue = $this->queueFactory
        ->get($form_state->getValue('queue_name'));
    // There is no harm in trying to recreate existing.
    $queue->createQueue();
    $count = $queue->numberOfItems();
    $item = $queue->claimItem(60);
    if (!empty($item)) {
        $this->messenger()
            ->addMessage($this->t('Claimed and deleted item id=@item_id string=@string for @seconds seconds. There are @count items in the queue.', [
            '@count' => $count,
            '@item_id' => $item->item_id,
            '@string' => $item->data,
            '@seconds' => $form_state->getValue('claim_time'),
        ]));
        $queue->deleteItem($item);
        $count = $queue->numberOfItems();
        $this->messenger()
            ->addMessage($this->t('There are now @count items in the queue.', [
            '@count' => $count,
        ]));
    }
    else {
        $count = $queue->numberOfItems();
        $this->messenger()
            ->addMessage($this->t('There were no items in the queue available to claim/delete. There are currently @count items in the queue.', [
            '@count' => $count,
        ]));
    }
    $form_state->setRebuild();
}