class EditorSelector

Selects an in-place editor (an Editor plugin) for a field.

Hierarchy

Expanded class hierarchy of EditorSelector

3 files declare their use of EditorSelector
EditIntegrationTest.php in drupal/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php
Contains \Drupal\editor\Tests\EditorIntegrationTest.
EditorSelectionTest.php in drupal/core/modules/edit/lib/Drupal/edit/Tests/EditorSelectionTest.php
Contains \Drupal\edit\Tests\EditorSelectionTest.
MetadataGeneratorTest.php in drupal/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php
Contains \Drupal\edit\Tests\MetadataGeneratorTest.
1 string reference to 'EditorSelector'
edit.services.yml in drupal/core/modules/edit/edit.services.yml
drupal/core/modules/edit/edit.services.yml
1 service uses EditorSelector

File

drupal/core/modules/edit/lib/Drupal/edit/EditorSelector.php, line 17
Contains \Drupal\edit\EditorSelector.

Namespace

Drupal\edit
View source
class EditorSelector implements EditorSelectorInterface {

  /**
   * The manager for editor plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $editorManager;

  /**
   * A list of alternative editor plugin IDs, keyed by editor plugin ID.
   *
   * @var array
   */
  protected $alternatives;

  /**
   * Constructs a new EditorSelector.
   *
   * @param \Drupal\Component\Plugin\PluginManagerInterface
   *   The manager for editor plugins.
   */
  public function __construct(PluginManagerInterface $editor_manager) {
    $this->editorManager = $editor_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function getEditor($formatter_type, FieldInstance $instance, array $items) {

    // Build a static cache of the editors that have registered themselves as
    // alternatives to a certain editor.
    if (!isset($this->alternatives)) {
      $editors = $this->editorManager
        ->getDefinitions();
      foreach ($editors as $alternative_editor_id => $editor) {
        if (isset($editor['alternativeTo'])) {
          foreach ($editor['alternativeTo'] as $original_editor_id) {
            $this->alternatives[$original_editor_id][] = $alternative_editor_id;
          }
        }
      }
    }

    // Check if the formatter defines an appropriate in-place editor. For
    // example, text formatters displaying untrimmed text can choose to use the
    // 'direct' editor. If the formatter doesn't specify, fall back to the
    // 'form' editor, since that can work for any field. Formatter definitions
    // can use 'disabled' to explicitly opt out of in-place editing.
    $formatter_info = field_info_formatter_types($formatter_type);
    $editor_id = $formatter_info['edit']['editor'];
    if ($editor_id === 'disabled') {
      return;
    }
    elseif ($editor_id === 'form') {
      return 'form';
    }

    // No early return, so create a list of all choices.
    $editor_choices = array(
      $editor_id,
    );
    if (isset($this->alternatives[$editor_id])) {
      $editor_choices = array_merge($editor_choices, $this->alternatives[$editor_id]);
    }

    // Make a choice.
    foreach ($editor_choices as $editor_id) {
      $editor = $this->editorManager
        ->createInstance($editor_id);
      if ($editor
        ->isCompatible($instance, $items)) {
        return $editor_id;
      }
    }

    // We still don't have a choice, so fall back to the default 'form' editor.
    return 'form';
  }

  /**
   * {@inheritdoc}
   */
  public function getEditorAttachments(array $editor_ids) {
    $attachments = array();
    $editor_ids = array_unique($editor_ids);

    // Editor plugins' attachments.
    foreach ($editor_ids as $editor_id) {
      $editor = $this->editorManager
        ->createInstance($editor_id);
      $attachments[] = $editor
        ->getAttachments();
    }
    return NestedArray::mergeDeepArray($attachments);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EditorSelector::$alternatives protected property A list of alternative editor plugin IDs, keyed by editor plugin ID.
EditorSelector::$editorManager protected property The manager for editor plugins.
EditorSelector::getEditor public function Returns the in-place editor (an Editor plugin) to use for a field. Overrides EditorSelectorInterface::getEditor
EditorSelector::getEditorAttachments public function Returns the attachments for all editors. Overrides EditorSelectorInterface::getEditorAttachments
EditorSelector::__construct public function Constructs a new EditorSelector.