View source
<?php
namespace Drupal\webform\Plugin\WebformElement;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\WebformInterface;
use Drupal\webform\WebformSubmissionInterface;
class WebformCodeMirror extends WebformElementBase {
protected function defineDefaultProperties() {
$properties = [
'placeholder' => '',
'mode' => 'text',
'wrap' => TRUE,
] + parent::defineDefaultProperties();
unset($properties['format_items'], $properties['format_items_html'], $properties['format_items_text']);
return $properties;
}
protected function defineTranslatableProperties() {
return array_merge(parent::defineTranslatableProperties(), [
'default_value',
]);
}
protected function formatHtmlItem(array $element, WebformSubmissionInterface $webform_submission, array $options = []) {
$value = $this
->getValue($element, $webform_submission, $options);
if (empty($value)) {
return '';
}
$element += [
'#mode' => 'text',
];
$format = $this
->getItemFormat($element);
switch ($format) {
case 'code':
return [
'#theme' => 'webform_codemirror',
'#code' => $value,
'#type' => $element['#mode'],
];
default:
return parent::formatHtmlItem($element, $webform_submission, $options);
}
}
public function getItemDefaultFormat() {
return 'code';
}
public function getItemFormats() {
return parent::getItemFormats() + [
'code' => $this
->t('Code'),
];
}
public function preview() {
return parent::preview() + [
'#mode' => 'yaml',
];
}
public function getTestValues(array $element, WebformInterface $webform, array $options = []) {
$element += [
'#mode' => 'text',
];
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['default']['default_value']['#type'] = 'webform_codemirror';
$form['default']['default_value']['#rows'] = 3;
$form['codemirror'] = [
'#type' => 'fieldset',
'#title' => $this
->t('CodeMirror settings'),
];
$form['codemirror']['mode'] = [
'#title' => $this
->t('Mode'),
'#type' => 'select',
'#options' => [
'text' => $this
->t('Plain text'),
'yaml' => $this
->t('YAML'),
'html' => $this
->t('HTML'),
'htmlmixed' => $this
->t('HTML (CSS & JavaScript)'),
'css' => 'CSS',
'javascript' => 'JavaScript',
'php' => 'PHP',
'twig' => 'Twig',
],
'#required' => TRUE,
];
$form['codemirror']['wrap'] = [
'#title' => $this
->t('Wrap long lines of text'),
'#type' => 'checkbox',
'#return_value' => TRUE,
];
return $form;
}
}