// Only update if the editor is using the `uploadImage` toolbar item.
$settings =
$editor->
getSettings();
if (is_array($settings['toolbar'
]['items'
]) &&
in_array('uploadImage',
$settings['toolbar'
]['items'
], TRUE
)) { // Replace `uploadImage` with `drupalInsertImage`.
$settings['toolbar'
]['items'
] =
str_replace('uploadImage', 'drupalInsertImage',
$settings['toolbar'
]['items'
]);
// `<img data-entity-uuid data-entity-type>` are implicitly supported when
// uploads are enabled as the attributes are necessary for upload
// functionality. If uploads aren't enabled, these attributes must still
// be supported to ensure existing content that may have them (despite
// uploads being disabled) remains editable. In this use case, the
// attributes are added to the `ckeditor5_sourceEditing` allowed tags.
if (!
$editor->
getImageUploadSettings()['status'
]) { // Add `sourceEditing` toolbar item if it does not already exist.
if (!
in_array('sourceEditing',
$settings['toolbar'
]['items'
], TRUE
)) { $settings['toolbar'
]['items'
][] = '|';
$settings['toolbar'
]['items'
][] = 'sourceEditing';
// @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\SourceEditing::defaultConfiguration()
$settings['plugins'
]['ckeditor5_sourceEditing'
] =
['allowed_tags' =>
[]];
} // Update configuration.
$settings['plugins'
]['ckeditor5_sourceEditing'
]['allowed_tags'
] = HTMLRestrictions::
fromString(implode(' ',
$settings['plugins'
]['ckeditor5_sourceEditing'
]['allowed_tags'
])) ->
merge(HTMLRestrictions::
fromString('<img data-entity-uuid data-entity-type>'
)) ->
toCKEditor5ElementsArray();
}