You are here

public function QueueExampleForm::submitClaimItem in Examples for Developers 8

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

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

Class

QueueExampleForm
Form with examples on how to use queue.

Namespace

Drupal\queue_example\Forms

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