Created
August 26, 2017 05:44
-
-
Save mortona42/2e29b47e65d85c03de3bcef4342e7953 to your computer and use it in GitHub Desktop.
D8 Database Queue Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Drupal\queue_example\Form; | |
use Drupal\Core\Form\FormBase; | |
use Drupal\Core\Form\FormStateInterface; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Core\Queue\QueueDatabaseFactory; | |
/** | |
* Class QueueExampleForm. | |
*/ | |
class QueueExample extends FormBase { | |
const QUEUE_NAME = 'item_queue'; | |
protected $databaseQueue; | |
/** | |
* Get DatabaseQueue from factory. | |
*/ | |
public function __construct(QueueDatabaseFactory $queue_database) { | |
$this->databaseQueue = $queue_database->get($this::QUEUE_NAME); | |
} | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('queue.database') | |
); | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function getFormId() { | |
return 'queue_example'; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public function buildForm(array $form, FormStateInterface $form_state) { | |
$item_count = $this->databaseQueue->numberOfItems(); | |
$form['item_count'] = [ | |
'#type' => 'item', | |
'#title' => $this->t('Item Count'), | |
'#markup' => $this->databaseQueue->numberOfItems(), | |
]; | |
$form['new_item'] = [ | |
'#type' => 'textfield', | |
'#title' => $this->t('New Item'), | |
]; | |
$form['add_item'] = [ | |
'#type' => 'submit', | |
'#value' => $this->t('Add Item'), | |
'#submit' => [[$this, 'submitAddItem']], | |
]; | |
$form['remove_item'] = [ | |
'#type' => 'submit', | |
'#value' => $this->t('Remove Item'), | |
'#submit' => [[$this, 'submitRemoveItem']], | |
]; | |
$form['clear_queue'] = [ | |
'#type' => 'submit', | |
'#value' => $this->t('Clear Queue'), | |
]; | |
return $form; | |
} | |
/** | |
* Add a queue item. | |
*/ | |
public function submitAddItem(array &$form, FormStateInterface $form_state) { | |
$item = $form_state->getValue('new_item'); | |
if ($item) { | |
$this->databaseQueue->createItem($item); | |
} | |
} | |
/** | |
* Remove a queue item. | |
*/ | |
public function submitRemoveItem(array &$form, FormStateInterface $form_state) { | |
if ($item = $this->databaseQueue->claimItem()) { | |
drupal_set_message($this->t('Removed @item', ['@item' => $item->data])); | |
$this->databaseQueue->deleteItem($item); | |
} | |
} | |
/** | |
* Clear queue items, uses default form submit function. | |
* | |
* {@inheritdoc} | |
*/ | |
public function submitForm(array &$form, FormStateInterface $form_state) { | |
$count = 0; | |
while ($item = $this->databaseQueue->claimItem()) { | |
$this->databaseQueue->deleteItem($item); | |
$count++; | |
} | |
drupal_set_message($this->t('Cleared @count items', ['@count' => $count])); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment