YamlFormUiOptionsForm.php in YAML Form 8
File
modules/yamlform_ui/src/YamlFormUiOptionsForm.php
View source
<?php
namespace Drupal\yamlform_ui;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\Element\YamlFormOptions;
use Drupal\yamlform\YamlFormOptionsForm;
class YamlFormUiOptionsForm extends YamlFormOptionsForm {
public function editForm(array $form, FormStateInterface $form_state) {
$form['options'] = [
'#type' => 'yamlform_options',
'#mode' => 'yaml',
'#title' => $this
->t('Options'),
'#title_display' => 'invisible',
'#empty_options' => 10,
'#add_more' => 10,
'#required' => TRUE,
'#default_value' => $this
->getOptions($form, $form_state),
];
return $form;
}
protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
if (isset($values['options']['options'])) {
$options = is_array($values['options']['options']) ? YamlFormOptions::convertValuesToOptions($values['options']['options']) : [];
}
elseif (isset($values['options'])) {
$options = is_array($values['options']) ? $values['options'] : [];
}
else {
$options = [];
}
$entity
->set('options', Yaml::encode($options));
unset($values['options']);
foreach ($values as $key => $value) {
$entity
->set($key, $value);
}
}
}