function QueueExampleForm::submitClaimItem

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

Submit function for the "claim" button.

Claims (retrieves) an item from the queue and reports the results.

Parameters

array $form: Form definition array.

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

File

modules/queue_example/src/Form/QueueExampleForm.php, line 328

Class

QueueExampleForm
Form with examples on how to use queue.

Namespace

Drupal\queue_example\Form

Code

public function submitClaimItem(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();
    $item = $queue->claimItem($form_state->getValue('claim_time'));
    $count = $queue->numberOfItems();
    if (!empty($item)) {
        $this->messenger()
            ->addMessage($this->t('Claimed 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'),
        ]));
    }
    else {
        $this->messenger()
            ->addMessage($this->t('There were no items in the queue available to claim. There are @count items in the queue.', [
            '@count' => $count,
        ]));
    }
    $form_state->setRebuild();
}