AuthenticationMultivalueKeyType.php in Key 8
File
src/Plugin/KeyType/AuthenticationMultivalueKeyType.php
View source
<?php
namespace Drupal\key\Plugin\KeyType;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\FormStateInterface;
use Drupal\key\Plugin\KeyTypeBase;
use Drupal\key\Plugin\KeyTypeMultivalueInterface;
class AuthenticationMultivalueKeyType extends KeyTypeBase implements KeyTypeMultivalueInterface {
public static function generateKeyValue(array $configuration) {
return '[]';
}
public function validateKeyValue(array $form, FormStateInterface $form_state, $key_value) {
if (empty($key_value)) {
return;
}
if (isset($form['settings']['input_section']['key_input_settings']['key_value'])) {
$error_element = $form['settings']['input_section']['key_input_settings']['key_value'];
}
else {
$error_element = $form;
}
$value = $this
->unserialize($key_value);
if ($value === NULL) {
$form_state
->setError($error_element, $this
->t('The key value does not contain valid JSON.'));
return;
}
$definition = $this
->getPluginDefinition();
$fields = $definition['multivalue']['fields'];
foreach ($fields as $id => $field) {
if (!is_array($field)) {
$field = [
'label' => $field,
];
}
if (isset($field['required']) && $field['required'] === FALSE) {
continue;
}
if (!isset($value[$id])) {
$form_state
->setError($error_element, $this
->t('The key value is missing the field %field.', [
'%field' => $id,
]));
}
elseif (empty($value[$id])) {
$form_state
->setError($error_element, $this
->t('The key value field %field is empty.', [
'%field' => $id,
]));
}
}
}
public function serialize(array $array) {
return Json::encode($array);
}
public function unserialize($value) {
return Json::decode($value);
}
}