YamlFormCodeMirror.php in YAML Form 8
File
src/Plugin/YamlFormElement/YamlFormCodeMirror.php
View source
<?php
namespace Drupal\yamlform\Plugin\YamlFormElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\yamlform\YamlFormElementBase;
use Drupal\yamlform\YamlFormInterface;
class YamlFormCodeMirror extends YamlFormElementBase {
public function getDefaultProperties() {
return parent::getDefaultProperties() + [
'mode' => 'text',
];
}
public function formatHtml(array &$element, $value, array $options = []) {
if (empty($value)) {
return '';
}
$format = $this
->getFormat($element);
switch ($format) {
case 'code':
return [
'#theme' => 'yamlform_codemirror',
'#code' => $value,
'#type' => $element['#mode'],
];
default:
return parent::formatHtml($element, $value, $options);
}
}
public function getDefaultFormat() {
return 'code';
}
public function getFormats() {
return parent::getFormats() + [
'code' => $this
->t('Code'),
];
}
public function getTestValue(array $element, YamlFormInterface $yamlform) {
switch ($element['#mode']) {
case 'html':
return '<p><b>Hello World!!!</b></p>';
case 'yaml':
return "message: 'Hello World'";
case 'text':
return "Hello World";
default:
return '';
}
}
public function form(array $form, FormStateInterface $form_state) {
$form = parent::form($form, $form_state);
$form['codemirror'] = [
'#type' => 'fieldset',
'#title' => $this
->t('CodeMirror settings'),
];
$form['codemirror']['mode'] = [
'#title' => $this
->t('Mode'),
'#type' => 'select',
'#options' => [
'yaml' => $this
->t('YAML'),
'html' => $this
->t('HTML'),
'text' => $this
->t('Plain text'),
],
'#required' => TRUE,
];
return $form;
}
}