View source
<?php
namespace Drupal\webform\Element;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\FormElement;
use Drupal\webform\Utility\WebformElementHelper;
use Drupal\webform\Utility\WebformFormHelper;
use Drupal\webform\Utility\WebformXss;
class WebformHtmlEditor extends FormElement {
public function getInfo() {
$class = get_class($this);
return [
'#input' => TRUE,
'#process' => [
[
$class,
'processWebformHtmlEditor',
],
[
$class,
'processAjaxForm',
],
[
$class,
'processGroup',
],
],
'#pre_render' => [
[
$class,
'preRenderGroup',
],
],
'#theme_wrappers' => [
'form_element',
],
'#format' => '',
];
}
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$element += [
'#default_value' => '',
];
if ($input === FALSE) {
return [
'value' => $element['#default_value'],
];
}
else {
if (isset($input['value']['value'])) {
$input['value'] = $input['value']['value'];
}
return $input;
}
}
public static function processWebformHtmlEditor(array $element) {
$element['#tree'] = TRUE;
$element += [
'value' => [],
];
$properties = [
'#title',
'#required',
'#attributes',
'#default_value',
];
$element['value'] += array_intersect_key($element, array_combine($properties, $properties));
$element['value']['#title_display'] = 'invisible';
$element['#error_no_message'] = TRUE;
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformHtmlEditor',
]);
$disabled = \Drupal::config('webform.settings')
->get('html_editor.disabled') ?: $element['#format'] === FALSE;
if ($disabled) {
$element['value'] += [
'#type' => 'webform_codemirror',
'#mode' => 'html',
];
return $element;
}
$format = $element['#format'] ?: \Drupal::config('webform.settings')
->get('html_editor.element_format');
if ($format) {
$element['value'] += [
'#type' => 'text_format',
'#format' => $format,
'#allowed_formats' => [
$format,
],
'#prefix' => '<div data-webform-states-no-clear>',
'#suffix' => '</div>',
];
WebformElementHelper::fixStatesWrapper($element);
return $element;
}
$element['value'] += [
'#type' => 'textarea',
];
$element['value']['#attributes']['class'][] = 'js-html-editor';
$element['#attached']['library'][] = 'webform/webform.element.html_editor';
$element['#attached']['drupalSettings']['webform']['html_editor']['allowedContent'] = static::getAllowedContent();
$libraries_manager = \Drupal::service('webform.libraries_manager');
$libraries = $libraries_manager
->getLibraries(TRUE);
$element['#attached']['drupalSettings']['webform']['html_editor']['plugins'] = [];
foreach ($libraries as $library_name => $library) {
if (strpos($library_name, 'ckeditor.') === FALSE) {
continue;
}
$plugin_name = str_replace('ckeditor.', '', $library_name);
$plugin_path = $library['plugin_path'];
$plugin_url = $library['plugin_url'];
if (file_exists($plugin_path)) {
$element['#attached']['drupalSettings']['webform']['html_editor']['plugins'][$plugin_name] = base_path() . $plugin_path;
}
else {
$element['#attached']['drupalSettings']['webform']['html_editor']['plugins'][$plugin_name] = $plugin_url;
}
}
if (\Drupal::moduleHandler()
->moduleExists('imce') && \Drupal\imce\Imce::access()) {
$element['#attached']['library'][] = 'imce/drupal.imce.ckeditor';
$element['#attached']['drupalSettings']['webform']['html_editor']['ImceImageIcon'] = file_create_url(drupal_get_path('module', 'imce') . '/js/plugins/ckeditor/icons/imceimage.png');
}
if (!empty($element['#states'])) {
WebformFormHelper::processStates($element, '#wrapper_attributes');
}
return $element;
}
public static function validateWebformHtmlEditor(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value']['value'];
if (is_array($value)) {
$value = $value['value'];
}
else {
$value = trim($value);
}
$element['#value'] = $value;
$form_state
->setValueForElement($element, $value);
}
public static function getAllowedContent() {
$allowed_tags = \Drupal::config('webform.settings')
->get('element.allowed_tags');
switch ($allowed_tags) {
case 'admin':
$allowed_tags = Xss::getAdminTagList();
break;
case 'html':
$allowed_tags = Xss::getHtmlTagList();
break;
default:
$allowed_tags = preg_split('/ +/', $allowed_tags);
break;
}
foreach ($allowed_tags as $index => $allowed_tag) {
$allowed_tags[$index] .= '(*)[*]{*}';
}
return implode('; ', $allowed_tags);
}
public static function getAllowedTags() {
$allowed_tags = \Drupal::config('webform.settings')
->get('element.allowed_tags');
switch ($allowed_tags) {
case 'admin':
return WebformXss::getAdminTagList();
case 'html':
return WebformXss::getHtmlTagList();
default:
return preg_split('/ +/', $allowed_tags);
}
}
public static function checkMarkup($text, array $options = []) {
$options += [
'tidy' => \Drupal::config('webform.settings')
->get('html_editor.tidy'),
];
if ($options['tidy']) {
if (substr_count($text, '<p>') === 1 && preg_match('#^\\s*<p>.*</p>\\s*$#m', $text)) {
$text = preg_replace('#^\\s*<p>#', '', $text);
$text = preg_replace('#</p>\\s*$#', '', $text);
}
}
if ($format = \Drupal::config('webform.settings')
->get('html_editor.element_format')) {
return [
'#type' => 'processed_text',
'#text' => $text,
'#format' => $format,
];
}
else {
return [
'#theme' => 'webform_html_editor_markup',
'#markup' => $text,
'#allowed_tags' => static::getAllowedTags(),
];
}
}
public static function stripTags($text) {
$allowed_tags = '<' . implode('><', static::getAllowedTags()) . '>';
return strip_tags($text, $allowed_tags);
}
}