trait TextEditorObjectDependentValidatorTrait

Same name in other branches
  1. 9 core/modules/ckeditor5/src/Plugin/Validation/Constraint/TextEditorObjectDependentValidatorTrait.php \Drupal\ckeditor5\Plugin\Validation\Constraint\TextEditorObjectDependentValidatorTrait
  2. 10 core/modules/ckeditor5/src/Plugin/Validation/Constraint/TextEditorObjectDependentValidatorTrait.php \Drupal\ckeditor5\Plugin\Validation\Constraint\TextEditorObjectDependentValidatorTrait

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(in_array($this->context
                ->getRoot()
                ->getDataDefinition()
                ->getDataType(), [
                'editor.editor.*',
                'entity:editor',
            ], TRUE));
            $text_format = FilterFormat::load($this->context
                ->getRoot()
                ->get('format')
                ->getValue());
            // This validator must not complain about a missing text format.
            // @see \Drupal\Tests\editor\Kernel\EditorValidationTest::testInvalidFormat()
            if ($text_format === NULL) {
                $text_format = FilterFormat::create([]);
            }
        }
        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

Title Sort descending Modifiers Object type Summary
TextEditorObjectDependentValidatorTrait::createTextEditorObjectFromContext private function Creates a text editor object from the execution context.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.