function QueueExampleForm::submitClaimItem

Same name and namespace in other branches
  1. 3.x modules/queue_example/src/Form/QueueExampleForm.php \Drupal\queue_example\Form\QueueExampleForm::submitClaimItem()
  2. 8.x-1.x queue_example/src/Forms/QueueExampleForm.php \Drupal\queue_example\Forms\QueueExampleForm::submitClaimItem()

Submission handler 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 337

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();
}