You are here

class Editor in Drupal 10

Same name in this branch
  1. 10 core/modules/editor/src/Annotation/Editor.php \Drupal\editor\Annotation\Editor
  2. 10 core/modules/editor/src/Entity/Editor.php \Drupal\editor\Entity\Editor
  3. 10 core/modules/quickedit/src/Plugin/InPlaceEditor/Editor.php \Drupal\quickedit\Plugin\InPlaceEditor\Editor

Defines the formatted text in-place editor.

Plugin annotation


@InPlaceEditor(
  id = "editor",
  provider = "editor",
)

Hierarchy

Expanded class hierarchy of Editor

2 string references to 'Editor'
ContextualFiltersStringTest::setUp in core/modules/views/tests/src/Functional/Plugin/ContextualFiltersStringTest.php
user.role.editor.yml in core/profiles/demo_umami/config/install/user.role.editor.yml
core/profiles/demo_umami/config/install/user.role.editor.yml

File

core/modules/quickedit/src/Plugin/InPlaceEditor/Editor.php, line 19

Namespace

Drupal\quickedit\Plugin\InPlaceEditor
View source
class Editor extends PluginBase implements InPlaceEditorInterface {

  /**
   * {@inheritdoc}
   */
  public function isCompatible(FieldItemListInterface $items) {
    $field_definition = $items
      ->getFieldDefinition();

    // This editor is incompatible with multivalued fields.
    if ($field_definition
      ->getFieldStorageDefinition()
      ->getCardinality() != 1) {
      return FALSE;
    }
    elseif ($editor = editor_load($items[0]->format)) {
      $definition = \Drupal::service('plugin.manager.editor')
        ->getDefinition($editor
        ->getEditor());
      if ($definition['supports_inline_editing'] === TRUE) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getMetadata(FieldItemListInterface $items) {
    $format_id = $items[0]->format;
    $metadata['format'] = $format_id;
    $metadata['formatHasTransformations'] = $this
      ->textFormatHasTransformationFilters($format_id);
    return $metadata;
  }

  /**
   * Returns whether the text format has transformation filters.
   *
   * @param int $format_id
   *   A text format ID.
   *
   * @return bool
   */
  protected function textFormatHasTransformationFilters($format_id) {
    $format = FilterFormat::load($format_id);
    return (bool) count(array_intersect([
      FilterInterface::TYPE_TRANSFORM_REVERSIBLE,
      FilterInterface::TYPE_TRANSFORM_IRREVERSIBLE,
    ], $format
      ->getFiltertypes()));
  }

  /**
   * {@inheritdoc}
   */
  public function getAttachments() {
    $user = \Drupal::currentUser();
    $user_format_ids = array_keys(filter_formats($user));
    $manager = \Drupal::service('plugin.manager.editor');
    $definitions = $manager
      ->getDefinitions();

    // Filter the current user's formats to those that support inline editing.
    $formats = [];
    foreach ($user_format_ids as $format_id) {
      if ($editor = editor_load($format_id)) {
        $editor_id = $editor
          ->getEditor();
        if (isset($definitions[$editor_id]['supports_inline_editing']) && $definitions[$editor_id]['supports_inline_editing'] === TRUE) {
          $formats[] = $format_id;
        }
      }
    }

    // Get the attachments for all text editors that the user might use.
    $attachments = $manager
      ->getAttachments($formats);

    // Also include quickedit.module's formatted text editor.
    $attachments['library'][] = 'quickedit/quickedit.inPlaceEditor.formattedText';
    return $attachments;
  }

}

Members