getEditor example


  public function access(Editor $editor): AccessResultInterface {
    if ($editor->getEditor() !== 'ckeditor5') {
      return AccessResult::forbidden();
    }
    // @todo add current request as an argument after     // https://www.drupal.org/project/drupal/issues/2786941 has been resolved.     $request = $this->requestStack->getCurrentRequest();
    $uuid = $request->query->get('uuid');
    if (!$uuid || !Uuid::isValid($uuid)) {
      throw new BadRequestHttpException();
    }
    $media = $this->entityRepository->loadEntityByUuid('media', $uuid);
    if (!$media) {
      

}

/** * The image toolbar item changed from `uploadImage` to `drupalInsertImage`. */
function ckeditor5_post_update_image_toolbar_item(&$sandbox = []) {
  $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);

  $callback = function DEditor $editor) {
    // Only try to update editors using CKEditor 5.     if ($editor->getEditor() !== 'ckeditor5') {
      return FALSE;
    }

    $needs_update = FALSE;
    // 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
$editor->setEditor('ckeditor5');

    $source_editing_additions = HTMLRestrictions::emptySet();
    // Compute the appropriate settings based on the CKEditor 4 configuration     // if it exists.     $old_editor = $editor->id() ? Editor::load($editor->id()) : NULL;
    $old_editor_restrictions = $old_editor ? HTMLRestrictions::fromTextFormat($old_editor->getFilterFormat()) : HTMLRestrictions::emptySet();
    // @todo Remove in https://www.drupal.org/project/drupal/issues/3245351     if ($old_editor) {
      $editor->setImageUploadSettings($old_editor->getImageUploadSettings());
    }
    if ($old_editor && $old_editor->getEditor() === 'ckeditor') {
      [$upgraded_settings$messages] = $this->createSettingsFromCKEditor4($old_editor->getSettings(), HTMLRestrictions::fromTextFormat($old_editor->getFilterFormat()));
      $editor->setSettings($upgraded_settings);
      // *Before* determining which elements are still needed for this text       // format, ensure that all already enabled plugins that are configurable       // have valid settings.       // For all already enabled plugins, find the ones that are configurable,       // and add their default settings. For enabled plugins with element       // subsets, compute the appropriate settings to achieve the subset that       // matches the original text format restrictions.       $this->addDefaultSettingsForEnabledConfigurablePlugins($editor);
      $this->computeSubsetSettingForEnabledPluginsWithSubsets($editor$text_format);
    }
// \Drupal\filter\Element\TextFormat::processFormat() copies properties to     // the expanded 'value' to the child element, including the #pre_render     // property. Skip this text format widget, if it contains no 'format'.     if (!isset($element['format'])) {
      return $element;
    }
    $format_ids = array_keys($element['format']['format']['#options']);

    // Early-return if no text editor is associated with any of the text formats.     $editors = Editor::loadMultiple($format_ids);
    foreach ($editors as $key => $editor) {
      $definition = $this->pluginManager->getDefinition($editor->getEditor());
      if (!in_array($element['#base_type']$definition['supported_element_types'])) {
        unset($editors[$key]);
      }
    }
    if (count($editors) === 0) {
      return $element;
    }

    // Use a hidden element for a single text format.     $field_id = $element['value']['#id'];
    if (!$element['format']['format']['#access']) {
      
/** * Verifies unicorn editor configuration. * * @param string $format_id * The format machine name. * @param bool $ponies_too * The expected value of the ponies_too setting. */
  protected function verifyUnicornEditorConfiguration($format_id$ponies_too = TRUE) {
    $editor = editor_load($format_id);
    $settings = $editor->getSettings();
    $this->assertSame('unicorn', $editor->getEditor(), 'The text editor is configured correctly.');
    $this->assertSame($ponies_too$settings['ponies_too'], 'The text editor settings are stored correctly.');
    $this->drupalGet('admin/config/content/formats/manage/' . $format_id);
    $select = $this->assertSession()->selectExists('editor[editor]');
    $this->assertFalse($select->hasAttribute('disabled'));
    $options = $select->findAll('css', 'option');
    $this->assertCount(2, $options);
    $this->assertTrue($options[1]->isSelected(), 'Option 2 ("Unicorn Editor") is selected.');
  }

}

  public function getAttachments(array $format_ids) {
    $attachments = ['library' => []];

    $settings = [];
    foreach ($format_ids as $format_id) {
      $editor = editor_load($format_id);
      if (!$editor) {
        continue;
      }

      $plugin = $this->createInstance($editor->getEditor());
      $plugin_definition = $plugin->getPluginDefinition();

      // Libraries.       $attachments['library'] = array_merge($attachments['library']$plugin->getLibraries($editor));

      // Format-specific JavaScript settings.       $settings['editor']['formats'][$format_id] = [
        'format' => $format_id,
        'editor' => $editor->getEditor(),
        'editorSettings' => $plugin->getJSSettings($editor),
        'editorSupportsContentFiltering' => $plugin_definition['supports_content_filtering'],
        
    $this->assertConfigSchema(
      $this->typedConfig,
      $updated_text_editor->getConfigDependencyName(),
      $updated_text_editor->toArray()
    );

    // Save this to ensure the config export order is applied.     // @see \Drupal\Core\Config\StorableConfigBase::castValue()     $updated_text_editor->save();

    // We should now have the expected data in the Editor config entity.     $this->assertSame('ckeditor5', $updated_text_editor->getEditor());
    $this->assertSame($expected_ckeditor5_settings$updated_text_editor->getSettings());

    // If this text format already had a text editor, ensure that the settings     // do not match the original settings, but the image upload settings should     // not have been changed.     if ($text_editor !== NULL) {
      $this->assertNotSame($text_editor->getSettings()$updated_text_editor->getSettings());
      $this->assertSame($text_editor->getImageUploadSettings()$updated_text_editor->getImageUploadSettings());
    }

    // The resulting Editor config entity should be valid.
/** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
    $typed_config = $this->container->get('config.typed');
    foreach ($names as $name) {
      $config = $this->config($name);
      $this->assertConfigSchema($typed_config$name$config->get());
    }

    // Validate all configuration.     // @todo Generalize in https://www.drupal.org/project/drupal/issues/2164373     foreach (Editor::loadMultiple() as $editor) {
      // Currently only text editors using CKEditor 5 can be validated.       if ($editor->getEditor() !== 'ckeditor5') {
        continue;
      }

      $this->assertSame([]array_map(
        function DConstraintViolation $v) {
          return (string) $v->getMessage();
        },
        iterator_to_array(CKEditor5::validatePair(
          $editor,
          $editor->getFilterFormat()
        ))
      ));

  public static function validatePair(EditorInterface $text_editor, FilterFormatInterface $text_format, bool $all_compatibility_problems = TRUE): ConstraintViolationListInterface {
    if ($text_editor->getEditor() !== 'ckeditor5') {
      throw new \InvalidArgumentException('This text editor is not configured to use CKEditor 5.');
    }

    $typed_config_manager = \Drupal::getContainer()->get('config.typed');
    $typed_config = $typed_config_manager->createFromNameAndData(
      'ckeditor5_valid_pair__format_and_editor',
      [
        // A mix of:         // - editor.editor.*.settings — note that "settings" is top-level in         // editor.editor.*, and so it is here, so all validation constraints         // will continue to work fine.
/** * Access check based on whether image upload is enabled or not. * * @param \Drupal\editor\Entity\Editor $editor * The text editor for which an image upload is occurring. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */
  public function imageUploadEnabledAccess(Editor $editor) {
    if ($editor->getEditor() !== 'ckeditor5') {
      return AccessResult::forbidden();
    }
    if ($editor->getImageUploadSettings()['status'] !== TRUE) {
      return AccessResult::forbidden();
    }

    return AccessResult::allowed();
  }

  /** * Validates the file. * * @param \Drupal\file\FileInterface $file * The file entity to validate. * @param array $validators * An array of upload validators to pass to file_validate(). * * @return \Drupal\Core\Entity\EntityConstraintViolationListInterface * The list of constraint violations, if any. */

  protected static function getPluginConfiguration(EditorInterface $editor, string $plugin_id): array {
    if ($editor->getEditor() !== 'ckeditor5') {
      throw new \InvalidArgumentException('This method should only be called on text editor config entities using CKEditor 5.');
    }
    return $editor->getSettings()['plugins'][$plugin_id] ?? [];
  }

  /** * {@inheritdoc} */
  public function getToolbarItems(): array {
    return $this->mergeDefinitionValues('getToolbarItems', $this->getDefinitions());
  }

  
Home | Imprint | This part of the site doesn't use cookies.