Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / quickedit / src / EditorSelector.php
1 <?php
2
3 namespace Drupal\quickedit;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Component\Utility\NestedArray;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\Core\Field\FormatterPluginManager;
9
10 /**
11  * Selects an in-place editor (an InPlaceEditor plugin) for a field.
12  */
13 class EditorSelector implements EditorSelectorInterface {
14
15   /**
16    * The manager for editor plugins.
17    *
18    * @var \Drupal\Component\Plugin\PluginManagerInterface
19    */
20   protected $editorManager;
21
22   /**
23    * The manager for formatter plugins.
24    *
25    * @var \Drupal\Core\Field\FormatterPluginManager
26    */
27   protected $formatterManager;
28
29   /**
30    * A list of alternative editor plugin IDs, keyed by editor plugin ID.
31    *
32    * @var array
33    */
34   protected $alternatives;
35
36   /**
37    * Constructs a new EditorSelector.
38    *
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.
43    */
44   public function __construct(PluginManagerInterface $editor_manager, FormatterPluginManager $formatter_manager) {
45     $this->editorManager = $editor_manager;
46     $this->formatterManager = $formatter_manager;
47   }
48
49   /**
50    * {@inheritdoc}
51    */
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') {
61       return;
62     }
63     elseif ($editor_id === 'form') {
64       return 'form';
65     }
66
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]);
71     }
72
73     // Make a choice.
74     foreach ($editor_choices as $editor_id) {
75       $editor = $this->editorManager->createInstance($editor_id);
76       if ($editor->isCompatible($items)) {
77         return $editor_id;
78       }
79     }
80
81     // We still don't have a choice, so fall back to the default 'form' editor.
82     return 'form';
83   }
84
85   /**
86    * {@inheritdoc}
87    */
88   public function getEditorAttachments(array $editor_ids) {
89     $attachments = [];
90     $editor_ids = array_unique($editor_ids);
91
92     // Editor plugins' attachments.
93     foreach ($editor_ids as $editor_id) {
94       $editor = $this->editorManager->createInstance($editor_id);
95       $attachments[] = $editor->getAttachments();
96     }
97
98     return NestedArray::mergeDeepArray($attachments);
99   }
100
101 }