View source
<?php
namespace Drupal\encrypt\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\encrypt\EncryptService;
use Drupal\encrypt\Plugin\EncryptionMethodPluginFormInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EncryptionProfileForm extends EntityForm {
protected $configFactory;
protected $encryptService;
protected $editConfirmed = FALSE;
protected $originalProfile = NULL;
public function __construct(ConfigFactoryInterface $config_factory, EncryptService $encrypt_service) {
$this->configFactory = $config_factory;
$this->encryptService = $encrypt_service;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('encryption'));
}
public function buildForm(array $form, FormStateInterface $form_state) {
if ($form_state
->isRebuilding()) {
if ($form_state
->getTriggeringElement()['#name'] == 'encryption_method') {
$this
->updateEncryptionMethod($form_state);
}
}
elseif ($this->operation == "edit") {
$encryption_profile = $this->entity;
$this->originalProfile = clone $encryption_profile;
}
return parent::buildForm($form, $form_state);
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$encryption_profile = $this->entity;
if ($this->operation == "edit" && !$this->editConfirmed) {
$form['confirm_edit'] = [
'#type' => 'markup',
'#markup' => $this
->t('Be extremely careful when editing an encryption profile! It may result in making data encrypted with this profile unreadable. Are you sure you want to edit this profile?'),
'#prefix' => '<p>',
'#suffix' => '</p>',
];
return $form;
}
$form['label'] = [
'#type' => 'textfield',
'#title' => $this
->t('Label'),
'#maxlength' => 255,
'#default_value' => $encryption_profile
->label(),
'#description' => $this
->t("Label for the encryption profile."),
'#required' => TRUE,
];
$form['id'] = [
'#type' => 'machine_name',
'#default_value' => $encryption_profile
->id(),
'#machine_name' => [
'exists' => '\\Drupal\\encrypt\\Entity\\EncryptionProfile::load',
],
'#disabled' => !$encryption_profile
->isNew(),
];
$form['encryption'] = [
'#type' => 'container',
'#prefix' => '<div id="encrypt-settings">',
'#suffix' => '</div>',
];
$encryption_methods = $this->encryptService
->loadEncryptionMethods(FALSE);
$method_options = [];
if (!$encryption_profile
->isNew()) {
$method = $encryption_profile
->getEncryptionMethod();
$method_options[$method
->getPluginId()] = $method
->getLabel();
}
foreach ($encryption_methods as $plugin_id => $definition) {
$method_options[$plugin_id] = (string) $definition['title'];
}
$form['encryption']['encryption_method'] = [
'#type' => 'select',
'#title' => $this
->t('Encryption Method'),
'#description' => $this
->t('Select the method used for encryption'),
'#options' => $method_options,
'#required' => TRUE,
'#default_value' => $encryption_profile
->getEncryptionMethodId(),
'#ajax' => [
'callback' => [
$this,
'ajaxUpdateSettings',
],
'event' => 'change',
'wrapper' => 'encrypt-settings',
],
];
$form['encryption']['encryption_method_configuration'] = [
'#type' => 'container',
'#title' => $this
->t('Encryption method settings'),
'#title_display' => FALSE,
'#tree' => TRUE,
];
if ($encryption_profile
->getEncryptionMethod() instanceof EncryptionMethodPluginFormInterface) {
$plugin_form_state = $this
->createPluginFormState($form_state);
$form['encryption']['encryption_method_configuration'] += $encryption_profile
->getEncryptionMethod()
->buildConfigurationForm([], $plugin_form_state);
$form_state
->setValue('encryption_method_configuration', $plugin_form_state
->getValues());
}
$form['encryption']['encryption_key'] = [
'#type' => 'key_select',
'#title' => $this
->t('Encryption Key'),
'#required' => TRUE,
'#default_value' => $encryption_profile
->getEncryptionKeyId(),
];
$key_filters = [
'type_group' => 'encryption',
];
$form['encryption']['encryption_key']['#key_filters'] = $key_filters;
return $form;
}
public function actions(array $form, FormStateInterface $form_state) {
if ($this->operation == "edit" && !$this->editConfirmed) {
return [
'submit' => [
'#type' => 'submit',
'#value' => $this
->t('Edit'),
'#button_type' => 'primary',
'#submit' => [
[
$this,
'confirmEdit',
],
],
],
'cancel' => [
'#type' => 'link',
'#title' => $this
->t('Cancel'),
'#attributes' => [
'class' => [
'button',
],
],
'#url' => Url::fromRoute('entity.encryption_profile.collection'),
'#cache' => [
'contexts' => [
'url.query_args:destination',
],
],
],
];
}
else {
return parent::actions($form, $form_state);
}
}
protected function createPluginFormState(FormStateInterface $form_state) {
$plugin_form_state = clone $form_state;
$plugin_form_state
->setValues($form_state
->getValue('encryption_method_configuration', []));
return $plugin_form_state;
}
public function ajaxUpdateSettings(array &$form, FormStateInterface $form_state) {
return $form['encryption'];
}
protected function updateEncryptionMethod(FormStateInterface $form_state) {
$encryption_profile = $this->entity;
$plugin = $encryption_profile
->getEncryptionMethod();
$encryption_profile
->setEncryptionMethod($plugin);
if ($this->originalProfile && $this->originalProfile
->getEncryptionMethod()
->getPluginId() == $plugin
->getPluginId()) {
$configuration = $this->originalProfile
->getEncryptionMethod()
->getConfiguration();
}
else {
$configuration = $plugin
->defaultConfiguration();
}
$plugin
->setConfiguration($configuration);
$form_state
->setValue('encryption_method_configuration', []);
$form_state
->getUserInput()['encryption_method_configuration'] = [];
}
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
if (!$form_state
->isSubmitted()) {
return;
}
if ($this->operation == "edit" && !$this->editConfirmed) {
return;
}
if ($plugin = $this->entity
->getEncryptionMethod()) {
if ($plugin instanceof EncryptionMethodPluginFormInterface) {
$plugin_form_state = $this
->createPluginFormState($form_state);
$plugin
->validateConfigurationForm($form, $plugin_form_state);
$form_state
->setValue('encryption_method_configuration', $plugin_form_state
->getValues());
$this
->moveFormStateErrors($plugin_form_state, $form_state);
$this
->moveFormStateStorage($plugin_form_state, $form_state);
}
}
$form_state
->cleanValues();
$this->entity = $this
->buildEntity($form, $form_state);
$errors = $this->entity
->validate();
if ($errors) {
$form_state
->setErrorByName('encryption_key', implode(';', $errors));
}
}
public function submitForm(array &$form, FormStateInterface $form_state) {
if ($plugin = $this->entity
->getEncryptionMethod()) {
if ($plugin instanceof EncryptionMethodPluginFormInterface) {
$plugin_form_state = $this
->createPluginFormState($form_state);
$plugin
->submitConfigurationForm($form, $plugin_form_state);
$form_state
->setValue('encryption_method_configuration', $plugin_form_state
->getValues());
}
}
parent::submitForm($form, $form_state);
}
public function confirmEdit(array &$form, FormStateInterface $form_state) {
$this->editConfirmed = TRUE;
$form_state
->setRebuild();
}
public function save(array $form, FormStateInterface $form_state) {
$encryption_profile = $this->entity;
$status = $encryption_profile
->save();
if ($status) {
$this
->messenger()
->addMessage($this
->t('Saved the %label encryption profile.', [
'%label' => $encryption_profile
->label(),
]));
}
else {
$this
->messenger()
->addMessage($this
->t('The %label encryption profile was not saved.', [
'%label' => $encryption_profile
->label(),
]));
}
$form_state
->setRedirectUrl($encryption_profile
->toUrl('collection'));
}
protected function moveFormStateErrors(FormStateInterface $from, FormStateInterface $to) {
foreach ($from
->getErrors() as $name => $error) {
$to
->setErrorByName($name, $error);
}
}
protected function moveFormStateStorage(FormStateInterface $from, FormStateInterface $to) {
foreach ($from
->getStorage() as $index => $value) {
$to
->set($index, $value);
}
}
}