ckeditor5-1.0.x-dev/src/Commands/CKEditor5Commands.php
src/Commands/CKEditor5Commands.php
<?php
declare(strict_types=1);
namespace Drupal\ckeditor5\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\ckeditor5\Plugin\Editor\CKEditor5;
use Drupal\Component\Utility\Html;
use Drupal\editor\EditorInterface;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\filter\FilterFormatInterface;
use Drush\Commands\DrushCommands;
/**
* CKEditor 5 drush commands.
*/
final class CKEditor5Commands extends DrushCommands {
/**
* Audits existing text formats for CKEditor 5 compatibility.
*
* @command ckeditor5:audit
* @filter-output
*
* @option upgrade_only Analyzes text formats using CKEditor 5 text editors only.
*
* @default $options []
*
* @usage ckeditor5:audit
* Audits all text formats.
* @usage ckeditor5:audit --upgrade_only
* Audits only the text formats currently using CKEditor 4.
*
* @validate-module-enabled ckeditor5
*
* @field-labels
* format_id: Text format
* editor_plugin_id: Text editor
* compatible: Compatible?
* details: Compatibility details
* @default-fields format_id,editor_plugin_id,compatible,details
*
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* Audit results formatted as table.
*/
public function audit(array $options = ['upgrade_only' => self::OPT]) : RowsOfFields {
$table = [];
$text_formats = FilterFormat::loadMultiple();
foreach ($text_formats as $id => $text_format) {
assert($text_format instanceof FilterFormatInterface);
$editor = Editor::load($id);
$editor_plugin_id = $editor ? $editor->getEditor() : NULL;
// Only look at text formats currently using CKEditor 4.
if ($options['upgrade_only'] && $editor_plugin_id !== 'ckeditor') {
continue;
}
$reconfigured_text_editor = Editor::create([
'editor' => 'ckeditor5',
// @todo Update in https://www.drupal.org/project/ckeditor5/issues/3216015.
'settings' => \Drupal::service('plugin.manager.editor')->createInstance('ckeditor5')->getDefaultSettings(),
'image_upload' => $editor ? $editor->getImageUploadSettings() : [],
]);
assert($reconfigured_text_editor instanceof EditorInterface);
$violations = CKEditor5::validatePair($reconfigured_text_editor, $text_format);
$compatible = TRUE;
$details = [];
foreach ($violations as $violation) {
$validation_error = Html::decodeEntities(strip_tags(str_replace('<br>', "\n\n", $violation->getMessage()))) . "\n";
// If there is a fundamental incompatibility, set $compatible = FALSE
// and return this violation as the only detail.
if ($violation->getPropertyPath() === '') {
if ($compatible) {
$compatible = FALSE;
$details = [];
}
$details[] = $validation_error;
continue;
}
$details[] = $validation_error;
}
$table[] = [
'format_id' => $id,
'editor_plugin_id' => $editor_plugin_id,
'compatible' => $compatible ? '✅' : '❌',
'details' => implode("\n\n", $details),
];
}
return new RowsOfFields($table);
}
}
