3 namespace Drupal\quickedit;
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Field\FormatterPluginManager;
11 * Selects an in-place editor (an InPlaceEditor plugin) for a field.
13 class EditorSelector implements EditorSelectorInterface {
16 * The manager for editor plugins.
18 * @var \Drupal\Component\Plugin\PluginManagerInterface
20 protected $editorManager;
23 * The manager for formatter plugins.
25 * @var \Drupal\Core\Field\FormatterPluginManager.
27 protected $formatterManager;
30 * A list of alternative editor plugin IDs, keyed by editor plugin ID.
34 protected $alternatives;
37 * Constructs a new EditorSelector.
39 * @param \Drupal\Component\Plugin\PluginManagerInterface $editor_manager
40 * The manager for editor plugins.
41 * @param \Drupal\Core\Field\FormatterPluginManager $formatter_manager
42 * The manager for formatter plugins.
44 public function __construct(PluginManagerInterface $editor_manager, FormatterPluginManager $formatter_manager) {
45 $this->editorManager = $editor_manager;
46 $this->formatterManager = $formatter_manager;
52 public function getEditor($formatter_type, FieldItemListInterface $items) {
53 // Check if the formatter defines an appropriate in-place editor. For
54 // example, text formatters displaying plain text can choose to use the
55 // 'plain_text' editor. If the formatter doesn't specify, fall back to the
56 // 'form' editor, since that can work for any field. Formatter definitions
57 // can use 'disabled' to explicitly opt out of in-place editing.
58 $formatter_info = $this->formatterManager->getDefinition($formatter_type);
59 $editor_id = $formatter_info['quickedit']['editor'];
60 if ($editor_id === 'disabled') {
63 elseif ($editor_id === 'form') {
67 // No early return, so create a list of all choices.
68 $editor_choices = [$editor_id];
69 if (isset($this->alternatives[$editor_id])) {
70 $editor_choices = array_merge($editor_choices, $this->alternatives[$editor_id]);
74 foreach ($editor_choices as $editor_id) {
75 $editor = $this->editorManager->createInstance($editor_id);
76 if ($editor->isCompatible($items)) {
81 // We still don't have a choice, so fall back to the default 'form' editor.
88 public function getEditorAttachments(array $editor_ids) {
90 $editor_ids = array_unique($editor_ids);
92 // Editor plugins' attachments.
93 foreach ($editor_ids as $editor_id) {
94 $editor = $this->editorManager->createInstance($editor_id);
95 $attachments[] = $editor->getAttachments();
98 return NestedArray::mergeDeepArray($attachments);