comparison core/lib/Drupal/Core/Field/WidgetPluginManager.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 129ea1e6d783
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Core\Field;
4
5 use Drupal\Component\Plugin\Factory\DefaultFactory;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
9
10 /**
11 * Plugin type manager for field widgets.
12 *
13 * @ingroup field_widget
14 */
15 class WidgetPluginManager extends DefaultPluginManager {
16
17 /**
18 * The field type manager to define field.
19 *
20 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
21 */
22 protected $fieldTypeManager;
23
24 /**
25 * An array of widget options for each field type.
26 *
27 * @var array
28 */
29 protected $widgetOptions;
30
31 /**
32 * Constructs a WidgetPluginManager object.
33 *
34 * @param \Traversable $namespaces
35 * An object that implements \Traversable which contains the root paths
36 * keyed by the corresponding namespace to look for plugin implementations.
37 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
38 * Cache backend instance to use.
39 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
40 * The module handler.
41 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
42 * The 'field type' plugin manager.
43 */
44 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
45 parent::__construct('Plugin/Field/FieldWidget', $namespaces, $module_handler, 'Drupal\Core\Field\WidgetInterface', 'Drupal\Core\Field\Annotation\FieldWidget');
46
47 $this->setCacheBackend($cache_backend, 'field_widget_types_plugins');
48 $this->alterInfo('field_widget_info');
49 $this->fieldTypeManager = $field_type_manager;
50 }
51
52 /**
53 * Overrides PluginManagerBase::getInstance().
54 *
55 * @param array $options
56 * An array with the following key/value pairs:
57 * - field_definition: (FieldDefinitionInterface) The field definition.
58 * - form_mode: (string) The form mode.
59 * - prepare: (bool, optional) Whether default values should get merged in
60 * the 'configuration' array. Defaults to TRUE.
61 * - configuration: (array) the configuration for the widget. The
62 * following key value pairs are allowed, and are all optional if
63 * 'prepare' is TRUE:
64 * - type: (string) The widget to use. Defaults to the
65 * 'default_widget' for the field type. The default widget will also be
66 * used if the requested widget is not available.
67 * - settings: (array) Settings specific to the widget. Each setting
68 * defaults to the default value specified in the widget definition.
69 * - third_party_settings: (array) Settings provided by other extensions
70 * through hook_field_formatter_third_party_settings_form().
71 *
72 * @return \Drupal\Core\Field\WidgetInterface|null
73 * A Widget object or NULL when plugin is not found.
74 */
75 public function getInstance(array $options) {
76 // Fill in defaults for missing properties.
77 $options += [
78 'configuration' => [],
79 'prepare' => TRUE,
80 ];
81
82 $configuration = $options['configuration'];
83 $field_definition = $options['field_definition'];
84 $field_type = $field_definition->getType();
85
86 // Fill in default configuration if needed.
87 if ($options['prepare']) {
88 $configuration = $this->prepareConfiguration($field_type, $configuration);
89 }
90
91 $plugin_id = $configuration['type'];
92
93 // Switch back to default widget if either:
94 // - the configuration does not specify a widget class
95 // - the field type is not allowed for the widget
96 // - the widget is not applicable to the field definition.
97 $definition = $this->getDefinition($configuration['type'], FALSE);
98 if (!isset($definition['class']) || !in_array($field_type, $definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
99 // Grab the default widget for the field type.
100 $field_type_definition = $this->fieldTypeManager->getDefinition($field_type);
101 if (empty($field_type_definition['default_widget'])) {
102 return NULL;
103 }
104 $plugin_id = $field_type_definition['default_widget'];
105 }
106
107 $configuration += [
108 'field_definition' => $field_definition,
109 ];
110 return $this->createInstance($plugin_id, $configuration);
111 }
112
113 /**
114 * {@inheritdoc}
115 */
116 public function createInstance($plugin_id, array $configuration = []) {
117 $plugin_definition = $this->getDefinition($plugin_id);
118 $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
119
120 // If the plugin provides a factory method, pass the container to it.
121 if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
122 return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
123 }
124
125 return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings']);
126 }
127
128
129 /**
130 * Merges default values for widget configuration.
131 *
132 * @param string $field_type
133 * The field type.
134 * @param array $configuration
135 * An array of widget configuration.
136 *
137 * @return array
138 * The display properties with defaults added.
139 */
140 public function prepareConfiguration($field_type, array $configuration) {
141 // Fill in defaults for missing properties.
142 $configuration += [
143 'settings' => [],
144 'third_party_settings' => [],
145 ];
146 // If no widget is specified, use the default widget.
147 if (!isset($configuration['type'])) {
148 $field_type = $this->fieldTypeManager->getDefinition($field_type);
149 $configuration['type'] = isset($field_type['default_widget']) ? $field_type['default_widget'] : NULL;
150 }
151 // Filter out unknown settings, and fill in defaults for missing settings.
152 $default_settings = $this->getDefaultSettings($configuration['type']);
153 $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings;
154
155 return $configuration;
156 }
157
158 /**
159 * Returns an array of widget type options for a field type.
160 *
161 * @param string|null $field_type
162 * (optional) The name of a field type, or NULL to retrieve all widget
163 * options. Defaults to NULL.
164 *
165 * @return array
166 * If no field type is provided, returns a nested array of all widget types,
167 * keyed by field type human name.
168 */
169 public function getOptions($field_type = NULL) {
170 if (!isset($this->widgetOptions)) {
171 $options = [];
172 $field_types = $this->fieldTypeManager->getDefinitions();
173 $widget_types = $this->getDefinitions();
174 uasort($widget_types, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
175 foreach ($widget_types as $name => $widget_type) {
176 foreach ($widget_type['field_types'] as $widget_field_type) {
177 // Check that the field type exists.
178 if (isset($field_types[$widget_field_type])) {
179 $options[$widget_field_type][$name] = $widget_type['label'];
180 }
181 }
182 }
183 $this->widgetOptions = $options;
184 }
185 if (isset($field_type)) {
186 return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : [];
187 }
188
189 return $this->widgetOptions;
190 }
191
192 /**
193 * Returns the default settings of a field widget.
194 *
195 * @param string $type
196 * A field widget type name.
197 *
198 * @return array
199 * The widget type's default settings, as provided by the plugin
200 * definition, or an empty array if type or settings are undefined.
201 */
202 public function getDefaultSettings($type) {
203 $plugin_definition = $this->getDefinition($type, FALSE);
204 if (!empty($plugin_definition['class'])) {
205 $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
206 return $plugin_class::defaultSettings();
207 }
208
209 return [];
210 }
211
212 }