You are here

public function EditorSelector::getEditor in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/quickedit/src/EditorSelector.php \Drupal\quickedit\EditorSelector::getEditor()

Returns the in-place editor (an InPlaceEditor plugin) to use for a field.

Parameters

string $formatter_type: The field's formatter type name.

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be in-place edited.

Return value

string|null The editor to use, or NULL to not enable in-place editing.

Overrides EditorSelectorInterface::getEditor

File

core/modules/quickedit/src/EditorSelector.php, line 57
Contains \Drupal\quickedit\EditorSelector.

Class

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

Namespace

Drupal\quickedit

Code

public function getEditor($formatter_type, FieldItemListInterface $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
  // 'plain_text' 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 = $this->formatterManager
    ->getDefinition($formatter_type);
  $editor_id = $formatter_info['quickedit']['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($items)) {
      return $editor_id;
    }
  }

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