ConfigKeyProvider.php in Key 8
File
src/Plugin/KeyProvider/ConfigKeyProvider.php
View source
<?php
namespace Drupal\key\Plugin\KeyProvider;
use Drupal\Core\Form\FormStateInterface;
use Drupal\key\Exception\KeyValueNotSetException;
use Drupal\key\Plugin\KeyProviderBase;
use Drupal\key\Plugin\KeyPluginFormInterface;
use Drupal\key\Plugin\KeyProviderSettableValueInterface;
use Drupal\key\KeyInterface;
class ConfigKeyProvider extends KeyProviderBase implements KeyPluginFormInterface, KeyProviderSettableValueInterface {
public function defaultConfiguration() {
return [
'base64_encoded' => FALSE,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
if ($form_state
->getFormObject()
->getEntity()
->getKeyType()
->getPluginDefinition()['group'] == 'encryption') {
$form['base64_encoded'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Base64-encoded'),
'#description' => $this
->t('Checking this will store the key with Base64 encoding.'),
'#default_value' => isset($this
->getConfiguration()['base64_encoded']) ? $this
->getConfiguration()['base64_encoded'] : $this
->defaultConfiguration()['base64_encoded'],
];
}
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this
->setConfiguration($form_state
->getValues());
}
public function getKeyValue(KeyInterface $key) {
$key_value = isset($this->configuration['key_value']) ? $this->configuration['key_value'] : '';
if (isset($this->configuration['base64_encoded']) && $this->configuration['base64_encoded'] == TRUE) {
$key_value = base64_decode($key_value);
}
return $key_value;
}
public function setKeyValue(KeyInterface $key, $key_value) {
if (isset($this->configuration['base64_encoded']) && $this->configuration['base64_encoded'] == TRUE) {
$key_value = base64_encode($key_value);
}
$this->configuration['key_value'] = $key_value;
if (isset($this->configuration['key_value'])) {
return TRUE;
}
else {
throw new KeyValueNotSetException();
}
}
public function deleteKeyValue(KeyInterface $key) {
return TRUE;
}
public static function obscureKeyValue($key_value, array $options = []) {
return $key_value;
}
}