SettingsForm.php in Salesforce Suite 8.3
File
modules/salesforce_encrypt/src/Form/SettingsForm.php
View source
<?php
namespace Drupal\salesforce_encrypt\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Drupal\encrypt\EncryptionProfileManagerInterface;
use Drupal\salesforce\EntityNotFoundException;
use Drupal\salesforce_encrypt\Rest\EncryptedRestClientInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
class SettingsForm extends FormBase {
protected $encryptionProfileManager;
public function __construct(StateInterface $state, EncryptionProfileManagerInterface $encryptionProfileManager, EncryptedRestClientInterface $client) {
$this->encryptionProfileManager = $encryptionProfileManager;
$this->state = $state;
$this->client = $client;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('state'), $container
->get('encrypt.encryption_profile.manager'), $container
->get('salesforce.client'));
}
public function getFormId() {
return 'salesforce_encrypt_config';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$options = $this->encryptionProfileManager
->getEncryptionProfileNamesAsOptions();
$default = NULL;
try {
$profile = $this->client
->getEncryptionProfile();
if (!empty($profile)) {
$default = $profile
->id();
}
} catch (EntityNotFoundException $e) {
drupal_set_message($e
->getFormattableMessage(), 'error');
drupal_set_message($this
->t('Error while loading encryption profile. You will need to <a href=":encrypt">assign a new encryption profile</a>, then <a href=":oauth">re-authenticate to Salesforce</a>.', [
':encrypt' => Url::fromRoute('salesforce_encrypt.settings')
->toString(),
':oauth' => Url::fromRoute('salesforce.authorize')
->toString(),
]), 'error');
}
$form['profile'] = [
'#type' => 'select',
'#title' => $this
->t('Encryption Profile'),
'#description' => $this
->t('Choose an encryption profile with which to encrypt Salesforce information.'),
'#options' => $options,
'#default_value' => $default,
'#empty_option' => $this
->t('Do not use encryption'),
];
$form['actions']['#type'] = 'actions';
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this
->t('Save configuration'),
'#button_type' => 'primary',
];
$form['#theme'] = 'system_config_form';
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$old_profile_id = $this->state
->get('salesforce_encrypt.profile');
$profile_id = $form_state
->getValue('profile');
if ($old_profile_id == $profile_id) {
return;
}
$profile = $this->encryptionProfileManager
->getEncryptionProfile($profile_id);
if (empty($profile_id)) {
$this->client
->disableEncryption();
}
elseif (empty($old_profile_id)) {
$this->client
->enableEncryption($profile);
}
else {
$this->client
->disableEncryption();
$this->client
->enableEncryption($profile);
}
$this->state
->resetCache();
drupal_set_message($this
->t('The configuration options have been saved.'));
}
}