Additional #pre_render callback for 'text_format' elements.
function editor_pre_render_format($element) {
// Allow modules to programmatically enforce no client-side editor by setting
// the #editor property to FALSE.
if (isset($element['#editor']) && !$element['#editor']) {
return $element;
}
// filter_process_format() copies properties to the expanded 'value' child
// element. Skip this text format widget, if it contains no 'format' or when
// the current user does not have access to edit the value.
if (!isset($element['format']) || !empty($element['value']['#disabled'])) {
return $element;
}
$format_ids = array_keys($element['format']['format']['#options']);
// Early-return if no text editor is associated with any of the text formats.
if (count(entity_load_multiple('editor', $format_ids)) === 0) {
return $element;
}
// Use a hidden element for a single text format.
$field_id = $element['value']['#id'];
if (!$element['format']['format']['#access']) {
// Use the first (and only) available text format.
$format_id = $format_ids[0];
$element['format']['editor'] = array(
'#type' => 'hidden',
'#name' => $element['format']['format']['#name'],
'#value' => $format_id,
'#attributes' => array(
'class' => array(
'editor',
),
'data-editor-for' => $field_id,
),
);
}
else {
$element['format']['format']['#attributes']['class'][] = 'editor';
$element['format']['format']['#attributes']['data-editor-for'] = $field_id;
}
// Attach Text Editor module's (this module) library.
$element['#attached']['library'][] = array(
'editor',
'drupal.editor',
);
// Attach attachments for all available editors.
$manager = drupal_container()
->get('plugin.manager.editor');
$element['#attached'] = NestedArray::mergeDeep($element['#attached'], $manager
->getAttachments($format_ids));
return $element;
}