class WebformHtmlEditor in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Element/WebformHtmlEditor.php \Drupal\webform\Element\WebformHtmlEditor
Provides a webform element for entering HTML using CodeMirror, TextFormat, or custom CKEditor.
Plugin annotation
@FormElement("webform_html_editor");
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\webform\Element\WebformHtmlEditor
- class \Drupal\Core\Render\Element\FormElement implements FormElementInterface
- class \Drupal\Core\Render\Element\RenderElement implements ElementInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of WebformHtmlEditor
25 files declare their use of WebformHtmlEditor
- ActionWebformHandler.php in src/
Plugin/ WebformHandler/ ActionWebformHandler.php - Color.php in src/
Plugin/ WebformElement/ Color.php - OverrideWebformVariant.php in src/
Plugin/ WebformVariant/ OverrideWebformVariant.php - TextBase.php in src/
Plugin/ WebformElement/ TextBase.php - webform.theme.template.inc in includes/
webform.theme.template.inc - Preprocessors and helper functions to make theming easier.
26 #type uses of WebformHtmlEditor
- ActionWebformHandler::buildConfigurationForm in src/
Plugin/ WebformHandler/ ActionWebformHandler.php - Form constructor.
- EmailWebformHandler::buildConfigurationForm in src/
Plugin/ WebformHandler/ EmailWebformHandler.php - Form constructor.
- EmailWebformHandler::resendMessageForm in src/
Plugin/ WebformHandler/ EmailWebformHandler.php - Build resend message webform.
- ExampleWebformVariant::buildConfigurationForm in modules/
webform_example_variant/ src/ Plugin/ WebformVariant/ ExampleWebformVariant.php - Form constructor.
- RemotePostWebformHandler::buildConfigurationForm in src/
Plugin/ WebformHandler/ RemotePostWebformHandler.php - Form constructor.
File
- src/
Element/ WebformHtmlEditor.php, line 17
Namespace
Drupal\webform\ElementView source
class WebformHtmlEditor extends FormElement {
/**
* {@inheritdoc}
*/
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' => '',
];
}
/**
* {@inheritdoc}
*/
public static function valueCallback(&$element, $input, FormStateInterface $form_state) {
$element += [
'#default_value' => '',
];
if ($input === FALSE) {
return [
'value' => $element['#default_value'],
];
}
else {
// Get value from TextFormat element.
if (isset($input['value']['value'])) {
$input['value'] = $input['value']['value'];
}
return $input;
}
}
/**
* Prepares a #type 'webform_html_editor' render element for input.html.twig.
*
* @param array $element
* An associative array containing the properties of the element.
*
* @return array
* The HTML Editor which can be a CodeMirror element, TextFormat, or
* Textarea which is transformed into a custom HTML Editor.
*/
public static function processWebformHtmlEditor(array $element) {
$element['#tree'] = TRUE;
// Define value element.
$element += [
'value' => [],
];
// Copy properties to value element.
$properties = [
'#title',
'#required',
'#attributes',
'#default_value',
];
$element['value'] += array_intersect_key($element, array_combine($properties, $properties));
// Hide title.
$element['value']['#title_display'] = 'invisible';
// Don't display inline form error messages.
$element['#error_no_message'] = TRUE;
// Add validate callback.
$element += [
'#element_validate' => [],
];
array_unshift($element['#element_validate'], [
get_called_class(),
'validateWebformHtmlEditor',
]);
// If HTML disabled and no #format is specified return simple CodeMirror
// HTML editor.
$disabled = \Drupal::config('webform.settings')
->get('html_editor.disabled') ?: $element['#format'] === FALSE;
if ($disabled) {
$element['value'] += [
'#type' => 'webform_codemirror',
'#mode' => 'html',
];
return $element;
}
// If #format or 'webform.settings.html_editor.element_format' is defined return
// a 'text_format' 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,
],
// Do not allow the text format value to be cleared when the text format
// is hidden via #states. We must use a wrapper <div> because
// The TextFormat element does not support #attributes.
// @see \Drupal\webform\Plugin\WebformElement\TextFormat::preRenderFixTextFormatStates
// @see \Drupal\filter\Element\TextFormat
'#prefix' => '<div data-webform-states-no-clear>',
'#suffix' => '</div>',
];
WebformElementHelper::fixStatesWrapper($element);
return $element;
}
// Else use textarea with completely custom HTML Editor.
$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();
/** @var \Drupal\webform\WebformLibrariesManagerInterface $libraries_manager */
$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;
}
/**
* Webform element validation handler for #type 'webform_html_editor'.
*/
public static function validateWebformHtmlEditor(&$element, FormStateInterface $form_state, &$complete_form) {
$value = $element['#value']['value'];
if (is_array($value)) {
// Get value from TextFormat element.
$value = $value['value'];
}
else {
$value = trim($value);
}
$element['#value'] = $value;
$form_state
->setValueForElement($element, $value);
}
/**
* Get allowed content.
*
* @return array
* Allowed content (tags) for CKEditor.
*/
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);
}
/**
* Get allowed tags.
*
* @return array
* 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);
}
}
/**
* Runs HTML markup through (optional) text format.
*
* @param string $text
* The text to be filtered.
* @param array $options
* HTML markup options.
*
* @return array
* Render array containing 'processed_text'.
*
* @see \Drupal\webform\Plugin\WebformHandler\EmailWebformHandler::getMessage
*/
public static function checkMarkup($text, array $options = []) {
$options += [
'tidy' => \Drupal::config('webform.settings')
->get('html_editor.tidy'),
];
// Remove <p> tags around a single line of text, which creates minor
// margin issues.
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(),
];
}
}
/**
* Strip dis-allowed HTML tags from HTML text.
*
* @param string $text
* HTML text.
*
* @return string
* HTML text with dis-allowed HTML tags removed.
*/
public static function stripTags($text) {
$allowed_tags = '<' . implode('><', static::getAllowedTags()) . '>';
return strip_tags($text, $allowed_tags);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormElement:: |
public static | function | Adds autocomplete functionality to elements. | |
FormElement:: |
public static | function | #process callback for #pattern form element property. | |
FormElement:: |
public static | function | #element_validate callback for #pattern form element property. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
2 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
PluginBase:: |
public | function | Constructs a \Drupal\Component\Plugin\PluginBase object. | 98 |
RenderElement:: |
public static | function | Adds Ajax information about an element to communicate with JavaScript. | |
RenderElement:: |
public static | function | Adds members of this group as actual elements for rendering. | |
RenderElement:: |
public static | function | Form element processing handler for the #ajax form property. | 1 |
RenderElement:: |
public static | function | Arranges elements into groups. | |
RenderElement:: |
public static | function |
Sets a form element's class attribute. Overrides ElementInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebformHtmlEditor:: |
public static | function | Runs HTML markup through (optional) text format. | |
WebformHtmlEditor:: |
public static | function | Get allowed content. | |
WebformHtmlEditor:: |
public static | function | Get allowed tags. | |
WebformHtmlEditor:: |
public | function |
Returns the element properties for this element. Overrides ElementInterface:: |
|
WebformHtmlEditor:: |
public static | function | Prepares a #type 'webform_html_editor' render element for input.html.twig. | |
WebformHtmlEditor:: |
public static | function | Strip dis-allowed HTML tags from HTML text. | |
WebformHtmlEditor:: |
public static | function | Webform element validation handler for #type 'webform_html_editor'. | |
WebformHtmlEditor:: |
public static | function |
Determines how user input is mapped to an element's #value property. Overrides FormElement:: |