You are here

class Editor in Zircon Profile 8

Same name in this branch
  1. 8 core/modules/editor/src/Annotation/Editor.php \Drupal\editor\Annotation\Editor
  2. 8 core/modules/editor/src/Entity/Editor.php \Drupal\editor\Entity\Editor
  3. 8 core/modules/editor/src/Plugin/InPlaceEditor/Editor.php \Drupal\editor\Plugin\InPlaceEditor\Editor
Same name and namespace in other branches
  1. 8.0 core/modules/editor/src/Plugin/InPlaceEditor/Editor.php \Drupal\editor\Plugin\InPlaceEditor\Editor

Defines the formatted text in-place editor.

Plugin annotation


@InPlaceEditor(
  id = "editor",
  alternativeTo = {"plain_text"}
)

Hierarchy

Expanded class hierarchy of Editor

File

core/modules/editor/src/Plugin/InPlaceEditor/Editor.php, line 23
Contains \Drupal\editor\Plugin\InPlaceEditor\Editor.

Namespace

Drupal\editor\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 (in_array($field_definition
      ->getType(), array(
      'text',
      'text_long',
      'text_with_summary',
    ), TRUE)) {
      if ($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}
   */
  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.
   */
  protected function textFormatHasTransformationFilters($format_id) {
    $format = entity_load('filter_format', $format_id);
    return (bool) count(array_intersect(array(
      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 = array();
    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 editor.module's formatted text editor.
    $attachments['library'][] = 'editor/quickedit.inPlaceEditor.formattedText';
    return $attachments;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Editor::getAttachments public function Returns the attachments for this editor. Overrides InPlaceEditorInterface::getAttachments
Editor::getMetadata function Generates metadata that is needed specifically for this editor. Overrides InPlaceEditorInterface::getMetadata
Editor::isCompatible public function Checks whether this in-place editor is compatible with a given field. Overrides InPlaceEditorInterface::isCompatible
Editor::textFormatHasTransformationFilters protected function Returns whether the text format has transformation filters.
PluginBase::$configuration protected property Configuration information passed into the plugin. 2
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. 69