View source
<?php
namespace Drupal\yamlform\Element;
use Drupal\Core\Serialization\Yaml;
use Drupal\Core\Render\Element\Textarea;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\Utility\YamlFormTidy;
class YamlFormCodeMirror extends Textarea {
protected static $modes = [
'css' => 'text/css',
'html' => 'text/html',
'javascript' => 'text/javascript',
'text' => 'text/plain',
'yaml' => 'text/x-yaml',
];
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#mode' => 'text',
'#skip_validation' => FALSE,
'#cols' => 60,
'#rows' => 5,
'#resizable' => 'vertical',
'#process' => [
[
$class,
'processYamlFormCodeMirror',
],
[
$class,
'processAjaxForm',
],
[
$class,
'processGroup',
],
],
'#pre_render' => [
[
$class,
'preRenderYamlFormCodeMirror',
],
[
$class,
'preRenderGroup',
],
],
'#element_validate' => [
[
$class,
'validateYamlFormCodeMirror',
],
],
'#theme' => 'textarea',
'#theme_wrappers' => [
'form_element',
],
];
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
if ($input === FALSE && $element['#mode'] == 'yaml' && isset($element['#default_value'])) {
if (is_array($element['#default_value'])) {
$element['#default_value'] = YamlFormTidy::tidy(Yaml::encode($element['#default_value']));
}
if ($element['#default_value'] == '{ }') {
$element['#default_value'] = '';
}
return $element['#default_value'];
}
return NULL;
}
public static function processYamlFormCodeMirror(&$element, FormStateInterface $form_state, &$complete_form) {
if (empty($element['#mode']) || !isset(self::$modes[$element['#mode']])) {
$element['#mode'] = 'text';
}
return $element;
}
public static function preRenderYamlFormCodeMirror($element) {
static::setAttributes($element, [
'js-yamlform-codemirror',
'yamlform-codemirror',
$element['#mode'],
]);
$element['#attributes']['data-yamlform-codemirror-mode'] = static::getMode($element['#mode']);
$element['#attached']['library'][] = 'yamlform/yamlform.element.codemirror.' . $element['#mode'];
return $element;
}
public static function validateYamlFormCodeMirror(&$element, FormStateInterface $form_state, &$complete_form) {
if ($errors = static::getErrors($element, $form_state, $complete_form)) {
$build = [
'title' => [
'#markup' => t('%title is not valid.', [
'%title' => isset($element['#title']) ? $element['#title'] : t('YAML'),
]),
],
'errors' => [
'#theme' => 'item_list',
'#items' => $errors,
],
];
$form_state
->setError($element, \Drupal::service('renderer')
->render($build));
}
if ($element['#mode'] == 'yaml' && (isset($element['#default_value']) && is_array($element['#default_value']))) {
if (preg_match('/^- (.*?)\\s*$/', $element['#value'], $match)) {
$value = [
$match[1],
];
}
else {
$value = $element['#value'] ? Yaml::decode($element['#value']) : [];
}
$form_state
->setValueForElement($element, $value);
}
}
protected static function getErrors(&$element, FormStateInterface $form_state, &$complete_form) {
if (!empty($element['#skip_validation'])) {
return NULL;
}
switch ($element['#mode']) {
case 'html':
libxml_use_internal_errors(TRUE);
if (simplexml_load_string('<fragment>' . $element['#value'] . '</fragment>')) {
return NULL;
}
$errors = libxml_get_errors();
libxml_clear_errors();
if (!$errors) {
return NULL;
}
$messages = [];
foreach ($errors as $error) {
$messages[] = $error->message;
}
return $messages;
case 'yaml':
try {
$value = trim($element['#value']);
$data = Yaml::decode($value);
if (!is_array($data) && $value) {
throw new \Exception(t('YAML must contain an associative array of elements.'));
}
return NULL;
} catch (\Exception $exception) {
return [
$exception
->getMessage(),
];
}
default:
return NULL;
}
}
public static function getMode($mode) {
return isset(static::$modes[$mode]) ? static::$modes[$mode] : static::$modes['text'];
}
}