FieldEncryptDecryptForm.php in Field Encryption 8.2
File
src/Form/FieldEncryptDecryptForm.php
View source
<?php
namespace Drupal\field_encrypt\Form;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class FieldEncryptDecryptForm extends ConfirmFormBase {
protected $entityType;
protected $fieldName;
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'));
}
public function getFormId() {
return 'field_encrypt_decrypt_form';
}
public function getQuestion() {
return t('Are you sure you want to remove encryption for field %field on %entity_type?', array(
'%field' => $this->fieldName,
'%entity_type' => $this->entityType,
));
}
public function getCancelUrl() {
return new Url('field_encrypt.field_overview');
}
public function getConfirmText() {
return t('Remove field encryption');
}
public function getDescription() {
return t('This action removes field encryption from the specified field. Existing field data will be decrypted through a batch process.');
}
public function buildForm(array $form, FormStateInterface $form_state, $entity_type = NULL, $field_name = NULL) {
$this->entityType = $entity_type;
$this->fieldName = $field_name;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$storage = $this->entityTypeManager
->getStorage('field_storage_config');
$field_storage_config = $storage
->load($this->entityType . '.' . $this->fieldName);
$field_storage_config
->unsetThirdPartySetting('field_encrypt', 'encrypt');
$field_storage_config
->unsetThirdPartySetting('field_encrypt', 'properties');
$field_storage_config
->unsetThirdPartySetting('field_encrypt', 'encryption_profile');
$field_storage_config
->save();
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}