public static function WebformHtmlEditor::processWebformHtmlEditor in Webform 6.x
Same name and namespace in other branches
- 8.5 src/Element/WebformHtmlEditor.php \Drupal\webform\Element\WebformHtmlEditor::processWebformHtmlEditor()
Prepares a #type 'webform_html_editor' render element for input.html.twig.
Parameters
array $element: An associative array containing the properties of the element.
Return value
array The HTML Editor which can be a CodeMirror element, TextFormat, or Textarea which is transformed into a custom HTML Editor.
File
- src/
Element/ WebformHtmlEditor.php, line 68
Class
- WebformHtmlEditor
- Provides a webform element for entering HTML using CodeMirror, TextFormat, or custom CKEditor.
Namespace
Drupal\webform\ElementCode
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;
}