View source
<?php
namespace Drupal\yamlform\Element;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\Utility\YamlFormElementHelper;
class YamlFormOptions extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#label' => t('option'),
'#labels' => t('options'),
'#empty_items' => 5,
'#add_more' => 1,
'#process' => [
[
$class,
'processYamlFormOptions',
],
],
'#theme_wrappers' => [
'form_element',
],
];
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE) {
if (!isset($element['#default_value'])) {
return [];
}
$options = is_string($element['#default_value']) ? Yaml::decode($element['#default_value']) : $element['#default_value'];
if (self::hasOptGroup($options)) {
return $options;
}
return self::convertOptionsToValues($options);
}
elseif (is_array($input) && isset($input['options'])) {
return is_string($input['options']) ? Yaml::decode($input['options']) : $input['options'];
}
else {
return NULL;
}
}
public static function processYamlFormOptions(&$element, FormStateInterface $form_state, &$complete_form) {
$element['#tree'] = TRUE;
$element['#element_validate'] = [
[
get_called_class(),
'validateYamlFormOptions',
],
];
YamlFormElementHelper::fixStatesWrapper($element);
if (isset($element['#default_value']) && is_array($element['#default_value']) && self::hasOptGroup($element['#default_value'])) {
$element['options'] = [
'#type' => 'yamlform_codemirror',
'#mode' => 'yaml',
'#default_value' => Yaml::encode($element['#default_value']),
'#description' => t('Key-value pairs MUST be specified as "safe_key: \'Some readable options\'". Use of only alphanumeric characters and underscores is recommended in keys. One option per line.') . '<br/>' . t('Option groups can be created by using just the group name followed by indented group options.'),
];
return $element;
}
else {
$properties = [
'#label',
'#labels',
'#empty_items',
'#add_more',
];
$element['options'] = array_intersect_key($element, array_combine($properties, $properties)) + [
'#type' => 'yamlform_multiple',
'#header' => TRUE,
'#element' => [
'value' => [
'#type' => 'textfield',
'#title' => t('Option value'),
'#title_display' => t('invisible'),
'#placeholder' => t('Enter value'),
],
'text' => [
'#type' => 'textfield',
'#title' => t('Option text'),
'#title_display' => t('invisible'),
'#placeholder' => t('Enter text'),
],
],
'#default_value' => isset($element['#default_value']) ? self::convertOptionsToValues($element['#default_value']) : [],
];
return $element;
}
}
public static function validateYamlFormOptions(&$element, FormStateInterface $form_state, &$complete_form) {
$options_value = NestedArray::getValue($form_state
->getValues(), $element['options']['#parents']);
if (is_string($options_value)) {
$options = Yaml::decode($options_value);
}
else {
$options = self::convertValuesToOptions($options_value);
}
if (!empty($element['#required']) && empty($options)) {
if (isset($element['#required_error'])) {
$form_state
->setError($element, $element['#required_error']);
}
elseif (isset($element['#title'])) {
$form_state
->setError($element, t('@name field is required.', [
'@name' => $element['#title'],
]));
}
else {
$form_state
->setError($element);
}
return;
}
$form_state
->setValueForElement($element, $options);
}
public static function convertValuesToOptions(array $values = []) {
$options = [];
foreach ($values as $value) {
$option_value = $value['value'];
$option_text = $value['text'];
if ($option_value === '') {
$option_value = $option_text;
}
elseif ($option_text === '') {
$option_text = $option_value;
}
$options[$option_value] = $option_text;
}
return $options;
}
public static function convertOptionsToValues(array $options = []) {
$values = [];
foreach ($options as $value => $text) {
$values[] = [
'value' => $value,
'text' => $text,
];
}
return $values;
}
public static function hasOptGroup(array $options) {
foreach ($options as $option_text) {
if (is_array($option_text)) {
return TRUE;
}
}
return FALSE;
}
}