Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\quickedit;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\PluginManagerInterface;
|
Chris@0
|
6 use Drupal\Component\Utility\NestedArray;
|
Chris@0
|
7 use Drupal\Core\Field\FieldItemListInterface;
|
Chris@0
|
8 use Drupal\Core\Field\FormatterPluginManager;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Selects an in-place editor (an InPlaceEditor plugin) for a field.
|
Chris@0
|
12 */
|
Chris@0
|
13 class EditorSelector implements EditorSelectorInterface {
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * The manager for editor plugins.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @var \Drupal\Component\Plugin\PluginManagerInterface
|
Chris@0
|
19 */
|
Chris@0
|
20 protected $editorManager;
|
Chris@0
|
21
|
Chris@0
|
22 /**
|
Chris@0
|
23 * The manager for formatter plugins.
|
Chris@0
|
24 *
|
Chris@17
|
25 * @var \Drupal\Core\Field\FormatterPluginManager
|
Chris@0
|
26 */
|
Chris@0
|
27 protected $formatterManager;
|
Chris@0
|
28
|
Chris@0
|
29 /**
|
Chris@0
|
30 * A list of alternative editor plugin IDs, keyed by editor plugin ID.
|
Chris@0
|
31 *
|
Chris@0
|
32 * @var array
|
Chris@0
|
33 */
|
Chris@0
|
34 protected $alternatives;
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * Constructs a new EditorSelector.
|
Chris@0
|
38 *
|
Chris@0
|
39 * @param \Drupal\Component\Plugin\PluginManagerInterface $editor_manager
|
Chris@0
|
40 * The manager for editor plugins.
|
Chris@0
|
41 * @param \Drupal\Core\Field\FormatterPluginManager $formatter_manager
|
Chris@0
|
42 * The manager for formatter plugins.
|
Chris@0
|
43 */
|
Chris@0
|
44 public function __construct(PluginManagerInterface $editor_manager, FormatterPluginManager $formatter_manager) {
|
Chris@0
|
45 $this->editorManager = $editor_manager;
|
Chris@0
|
46 $this->formatterManager = $formatter_manager;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * {@inheritdoc}
|
Chris@0
|
51 */
|
Chris@0
|
52 public function getEditor($formatter_type, FieldItemListInterface $items) {
|
Chris@0
|
53 // Check if the formatter defines an appropriate in-place editor. For
|
Chris@0
|
54 // example, text formatters displaying plain text can choose to use the
|
Chris@0
|
55 // 'plain_text' editor. If the formatter doesn't specify, fall back to the
|
Chris@0
|
56 // 'form' editor, since that can work for any field. Formatter definitions
|
Chris@0
|
57 // can use 'disabled' to explicitly opt out of in-place editing.
|
Chris@0
|
58 $formatter_info = $this->formatterManager->getDefinition($formatter_type);
|
Chris@0
|
59 $editor_id = $formatter_info['quickedit']['editor'];
|
Chris@0
|
60 if ($editor_id === 'disabled') {
|
Chris@0
|
61 return;
|
Chris@0
|
62 }
|
Chris@0
|
63 elseif ($editor_id === 'form') {
|
Chris@0
|
64 return 'form';
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 // No early return, so create a list of all choices.
|
Chris@0
|
68 $editor_choices = [$editor_id];
|
Chris@0
|
69 if (isset($this->alternatives[$editor_id])) {
|
Chris@0
|
70 $editor_choices = array_merge($editor_choices, $this->alternatives[$editor_id]);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 // Make a choice.
|
Chris@0
|
74 foreach ($editor_choices as $editor_id) {
|
Chris@0
|
75 $editor = $this->editorManager->createInstance($editor_id);
|
Chris@0
|
76 if ($editor->isCompatible($items)) {
|
Chris@0
|
77 return $editor_id;
|
Chris@0
|
78 }
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 // We still don't have a choice, so fall back to the default 'form' editor.
|
Chris@0
|
82 return 'form';
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * {@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public function getEditorAttachments(array $editor_ids) {
|
Chris@0
|
89 $attachments = [];
|
Chris@0
|
90 $editor_ids = array_unique($editor_ids);
|
Chris@0
|
91
|
Chris@0
|
92 // Editor plugins' attachments.
|
Chris@0
|
93 foreach ($editor_ids as $editor_id) {
|
Chris@0
|
94 $editor = $this->editorManager->createInstance($editor_id);
|
Chris@0
|
95 $attachments[] = $editor->getAttachments();
|
Chris@0
|
96 }
|
Chris@0
|
97
|
Chris@0
|
98 return NestedArray::mergeDeepArray($attachments);
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 }
|