class FieldEncryptUpdateForm in Field Encryption 8.2
Form builder for the field_encrypt field update page.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\field_encrypt\Form\FieldEncryptUpdateForm
Expanded class hierarchy of FieldEncryptUpdateForm
1 string reference to 'FieldEncryptUpdateForm'
File
- src/
Form/ FieldEncryptUpdateForm.php, line 15
Namespace
Drupal\field_encrypt\FormView source
class FieldEncryptUpdateForm extends FormBase {
/**
* The queue factory.
*
* @var \Drupal\Core\Queue\QueueFactory
*/
protected $queueFactory;
/**
* The QueueWorker manager.
*
* @var \Drupal\Core\Queue\QueueWorkerManagerInterface
*/
protected $queueManager;
/**
* Constructs a new FieldEncryptUpdateForm.
*
* @param \Drupal\Core\Queue\QueueFactory $queue_factory
* The queue factory.
* @param \Drupal\Core\Queue\QueueWorkerManagerInterface $queue_manager
* The QueueWorker manager.
*/
public function __construct(QueueFactory $queue_factory, QueueWorkerManagerInterface $queue_manager) {
$this->queueFactory = $queue_factory;
$this->queueManager = $queue_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('queue'), $container
->get('plugin.manager.queue_worker'));
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'field_encrypt_field_update';
}
/**
* {@inheritdoc}
*/
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;
}
/**
* {@inheritdoc}
*/
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);
}
/**
* Processes batch updating of encryption fields.
*
* @param array $context
* The batch API context.
*/
public static function processBatch(&$context) {
$queue_factory = \Drupal::service('queue');
$queue_manager = \Drupal::service('plugin.manager.queue_worker');
$config = \Drupal::config('field_encrypt.settings');
/** @var QueueInterface $queue */
$queue = $queue_factory
->get('cron_encrypted_field_update');
$num_items = $queue
->numberOfItems();
/** @var QueueWorkerInterface $queue_worker */
$queue_worker = $queue_manager
->createInstance('cron_encrypted_field_update');
if (empty($context['sandbox'])) {
$context['sandbox']['progress'] = 0;
$context['sandbox']['max'] = $num_items;
}
// Process entities in groups. Default batch size is 5.
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();
}
}
}
// Inform the batch engine that we are not finished,
// and provide an estimation of the completion level we reached.
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
/**
* Finish batch encryption updates of fields.
*
* @param bool $success
* A boolean indicating whether the batch has completed successfully.
* @param array $results
* The value set in $context['results'] by callback_batch_operation().
* @param array $operations
* If $success is FALSE, contains the operations that remained unprocessed.
*/
public static function finishBatch($success, $results, $operations) {
// The 'success' parameter means no fatal PHP errors were detected. All
// other error management should be handled using 'results'.
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);
}
}
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FieldEncryptUpdateForm:: |
protected | property | The queue factory. | |
FieldEncryptUpdateForm:: |
protected | property | The QueueWorker manager. | |
FieldEncryptUpdateForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
FieldEncryptUpdateForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
FieldEncryptUpdateForm:: |
public static | function | Finish batch encryption updates of fields. | |
FieldEncryptUpdateForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
FieldEncryptUpdateForm:: |
public static | function | Processes batch updating of encryption fields. | |
FieldEncryptUpdateForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
FieldEncryptUpdateForm:: |
public | function | Constructs a new FieldEncryptUpdateForm. | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |