View source
<?php
namespace Drupal\field_encrypt\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Queue\QueueWorkerManagerInterface;
use Drupal\Core\Queue\SuspendQueueException;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldEncryptUpdateForm extends FormBase {
protected $queueFactory;
protected $queueManager;
public function __construct(QueueFactory $queue_factory, QueueWorkerManagerInterface $queue_manager) {
$this->queueFactory = $queue_factory;
$this->queueManager = $queue_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('queue'), $container
->get('plugin.manager.queue_worker'));
}
public function getFormId() {
return 'field_encrypt_field_update';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$queue = $this->queueFactory
->get('cron_encrypted_field_update');
$num_items = $queue
->numberOfItems();
$status_message = $message = $this
->formatPlural($num_items, 'There is one field queued for encryption updates.', 'There are @count fields queued for encryption updates.');
$form['status'] = array(
'#markup' => '<p>' . $status_message . '</p>',
);
if ($num_items > 0) {
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Update encryption on existing fields'),
);
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$batch = [
'title' => t('Updating field encryption'),
'operations' => array(
array(
array(
get_class($this),
'processBatch',
),
array(),
),
),
'finished' => array(
get_class($this),
'finishBatch',
),
];
batch_set($batch);
}
public static function processBatch(&$context) {
$queue_factory = \Drupal::service('queue');
$queue_manager = \Drupal::service('plugin.manager.queue_worker');
$config = \Drupal::config('field_encrypt.settings');
$queue = $queue_factory
->get('cron_encrypted_field_update');
$num_items = $queue
->numberOfItems();
$queue_worker = $queue_manager
->createInstance('cron_encrypted_field_update');
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $num_items;
}
for ($i = 1; $i <= $config
->get('batch_size'); $i++) {
if ($item = $queue
->claimItem()) {
try {
$queue_worker
->processItem($item->data);
$queue
->deleteItem($item);
$context['results']['items'][] = $item->data['entity_id'];
$message = t('Updating @field_name on @entity_type with ID @entity_id', array(
'@field_name' => $item->data['field_name'],
'@entity_type' => $item->data['entity_type'],
'@entity_id' => $item->data['entity_id'],
));
$context['message'] = $message;
$context['sandbox']['progress']++;
} catch (SuspendQueueException $e) {
$queue
->releaseItem($item);
} catch (\Exception $e) {
watchdog_exception('field_encrypt', $e);
if (!isset($context['results']['errors'])) {
$context['results']['errors'] = array();
}
$context['results']['errors'][] = $e
->getMessage();
}
}
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
public static function finishBatch($success, $results, $operations) {
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results['items']), 'One field updated.', '@count fields updated.');
\Drupal::messenger()
->addMessage($message);
}
else {
if (!empty($results['errors'])) {
foreach ($results['errors'] as $error) {
\Drupal::messenger()
->addError($error);
}
}
}
}
}