function QueueExampleForm::submitAddQueueItem
Same name in other branches
- 3.x modules/queue_example/src/Form/QueueExampleForm.php \Drupal\queue_example\Form\QueueExampleForm::submitAddQueueItem()
- 4.0.x modules/queue_example/src/Form/QueueExampleForm.php \Drupal\queue_example\Form\QueueExampleForm::submitAddQueueItem()
Submit function for the insert-into-queue button.
Parameters
array $form: Form definition array.
\Drupal\Core\Form\FormStateInterface $form_state: Form state object.
File
-
queue_example/
src/ Forms/ QueueExampleForm.php, line 297
Class
- QueueExampleForm
- Form with examples on how to use queue.
Namespace
Drupal\queue_example\FormsCode
public function submitAddQueueItem(array &$form, FormStateInterface $form_state) {
// Get a queue (of the default type) called 'queue_example_queue'.
// If the default queue class is SystemQueue this creates a queue that
// stores its items in the database.
$queue = $this->queueFactory
->get($form_state->getValue('queue_name'));
// There is no harm in trying to recreate existing.
$queue->createQueue();
// Queue the string.
$queue->createItem($form_state->getValue('string_to_add'));
$count = $queue->numberOfItems();
$this->messenger()
->addMessage($this->t('Queued your string (@string_to_add). There are now @count items in the queue.', [
'@count' => $count,
'@string_to_add' => $form_state->getValue('string_to_add'),
]));
// Allows us to keep information in $form_state.
$form_state->setRebuild();
// Unsetting the string_to_add allows us to set the incremented default
// value for the user so they don't have to type anything.
$form_state->unsetValue('string_to_add');
$form_state->set('insert_counter', $count + 1);
}