You are here

trait TextEditorObjectDependentValidatorTrait in Drupal 10

Some CKEditor 5 constraint validators need a Text Editor object.

Hierarchy

File

core/modules/ckeditor5/src/Plugin/Validation/Constraint/TextEditorObjectDependentValidatorTrait.php, line 15

Namespace

Drupal\ckeditor5\Plugin\Validation\Constraint
View source
trait TextEditorObjectDependentValidatorTrait {

  /**
   * Creates a text editor object from the execution context.
   *
   * Works both for an individual text editor config entity and a pair.
   *
   * @return \Drupal\editor\EditorInterface
   *   A text editor object, with the text format pre-populated.
   */
  private function createTextEditorObjectFromContext() : EditorInterface {
    if ($this->context
      ->getRoot()
      ->getDataDefinition()
      ->getDataType() === 'ckeditor5_valid_pair__format_and_editor') {
      $text_format = FilterFormat::create([
        'filters' => $this->context
          ->getRoot()
          ->get('filters')
          ->toArray(),
      ]);
    }
    else {
      assert($this->context
        ->getRoot()
        ->getDataDefinition()
        ->getDataType() === 'editor.editor.*');
      $text_format = FilterFormat::load($this->context
        ->getRoot()
        ->get('format')
        ->getValue());
    }
    assert($text_format instanceof FilterFormatInterface);
    $text_editor = Editor::create([
      'editor' => 'ckeditor5',
      'settings' => $this->context
        ->getRoot()
        ->get('settings')
        ->toArray(),
      'image_upload' => $this->context
        ->getRoot()
        ->get('image_upload')
        ->toArray(),
      // Specify `filterFormat` to ensure that the generated Editor config
      // entity object already has the $filterFormat property set, to prevent
      // calls to Editor::hasAssociatedFilterFormat() and
      // Editor::getFilterFormat() from loading the FilterFormat from storage.
      // As far as this validation constraint validator is concerned, the
      // concrete FilterFormat entity ID does not matter, all that matters is
      // its filter configuration. Those exist in $text_format.
      'filterFormat' => $text_format,
    ]);
    assert($text_editor instanceof EditorInterface);
    return $text_editor;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TextEditorObjectDependentValidatorTrait::createTextEditorObjectFromContext private function Creates a text editor object from the execution context.