1 ckeditor5.upgrade.inc | _ckeditor5_upgrade_format($original_format, &$warnings) |
Internal use only, performs the actual upgrade.
Parameters
stdClass $original_format: The text format to be upgraded.
array $warnings: Any warnings that occur during the upgrade. Modified by reference.
Return value
stdClass: The modified text format, ready to be saved with filter_format_save().
See also
File
- core/
modules/ ckeditor5/ ckeditor5.upgrade.inc, line 118 - CKEditor 4 to CKEditor 5 upgrade code.
Code
function _ckeditor5_upgrade_format($original_format, &$warnings) {
$format = clone($original_format);
foreach ($original_format->filters as $filter_name => $filter) {
$format->filters[$filter_name] = clone($filter);
}
$warnings = array(
'error' => NULL,
'removed_buttons' => array(),
);
if ($format->editor === 'ckeditor5') {
$warnings['error'] = t('The text format %format is already upgraded to CKEditor 5.', array('%format' => $format->name));
return FALSE;
}
elseif ($format->editor !== 'ckeditor') {
$warnings['error'] = t('The text format %format is not using the CKEditor 4 text editor.', array('%format' => $format->name));
return FALSE;
}
$format->editor = 'ckeditor5';
// Get a list of button mappings between CKEditor 4 and 5 modules.
$button_mapping = module_invoke_all('ckeditor5_upgrade_button_mapping');
backdrop_alter('ckeditor5_upgrade_button_mapping', $button_mapping);
// Convert the toolbar.
$new_toolbar = array();
$toolbar = $format->editor_settings['toolbar'];
$row_count = count($toolbar);
foreach ($toolbar as $row_index => $toolbar_row) {
$group_count = count($toolbar_row);
foreach ($toolbar_row as $group_index => $toolbar_group) {
$new_group = array();
foreach ($toolbar_group['items'] as $button_name) {
if (isset($button_mapping[$button_name])) {
$new_group[] = $button_mapping[$button_name];
}
else {
$warnings['removed_buttons'][] = $button_name;
}
}
// Add a separator between groups (but not at the end of the row).
if ($group_index < $group_count - 1) {
$new_group[] = '|';
}
$new_toolbar = array_merge($new_toolbar, $new_group);
}
// Add a line break between rows (except on the last row).
if ($row_index < $row_count - 1) {
$new_toolbar[] = '-';
}
}
// Clean up any empty groups. This can happen when buttons have no equivalent
// in CKEditor 5, resulting in empty groups and rows.
$prev_button = '';
foreach ($new_toolbar as $index => $button) {
// Remove empty groups.
if ($button === '|' && ($prev_button === '|' || $prev_button === '-')) {
unset($new_toolbar[$index]);
}
// Remove empty rows.
elseif ($button === '-' && $prev_button === '-') {
unset($new_toolbar[$index]);
}
else {
$prev_button = $button;
}
}
$new_toolbar = array_values($new_toolbar);
// Remove any trailing group separator.
if ($new_toolbar[count($new_toolbar) - 1] === '|') {
array_pop($new_toolbar);
}
$format->editor_settings['toolbar'] = $new_toolbar;
// Remove the unused format_list configuration.
if (isset($format->editor_settings['format_list'])) {
unset($format->editor_settings['format_list']);
}
// Convert the styles list.
if (!empty($format->editor_settings['plugins']['style']['style_list'])) {
$style_list = $format->editor_settings['plugins']['style']['style_list'];
foreach ($style_list as $style_item) {
$format->editor_settings['style_list'][] = array(
'name' => $style_item['name'],
'element' => $style_item['element'],
'classes' => explode('.', $style_item['attributes']['class']),
);
}
unset($format->editor_settings['plugins']['style']);
}
// Adjust the settings based on allowed tags if filtered_html is enabled.
if (!empty($format->filters['filter_html']->status)) {
// Create the heading list based on allowed HTML tags.
$tag_list = $format->filters['filter_html']->settings['allowed_html'];
$tags = explode(' ', $tag_list);
$new_tags = array();
foreach ($tags as $tag) {
$tag = str_replace(array('<', '>'), '', $tag);
if (substr($tag, 0, 1) === 'h' && strlen($tag) === 2) {
$new_tags[] = $tag;
}
}
sort($new_tags);
$format->editor_settings['heading_list'] = backdrop_map_assoc($new_tags);
// Add <u> to the list of allowed tags if the underline button is present.
if (in_array('underline', $new_toolbar) && !in_array('<u>', $new_tags)) {
$format->filters['filter_html']->settings['allowed_html'] = $tag_list . ' <u>';
}
}
else {
$format->editor_settings['heading_list'] = array(
'h1' => 'h1',
'h2' => 'h2',
'h3' => 'h3',
'h4' => 'h4',
'h5' => 'h5',
'h6' => 'h6',
);
}
// Allow other modules to further modify the upgraded text format.
backdrop_alter('ckeditor5_upgrade_format', $format, $original_format);
// Remove the now un-used "plugins" parent array.
unset($format->editor_settings['plugins']);
return $format;
}