Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Field / WidgetPluginManager.php
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    * Merges default values for widget configuration.
130    *
131    * @param string $field_type
132    *   The field type.
133    * @param array $configuration
134    *   An array of widget configuration.
135    *
136    * @return array
137    *   The display properties with defaults added.
138    */
139   public function prepareConfiguration($field_type, array $configuration) {
140     // Fill in defaults for missing properties.
141     $configuration += [
142       'settings' => [],
143       'third_party_settings' => [],
144     ];
145     // If no widget is specified, use the default widget.
146     if (!isset($configuration['type'])) {
147       $field_type = $this->fieldTypeManager->getDefinition($field_type);
148       $configuration['type'] = isset($field_type['default_widget']) ? $field_type['default_widget'] : NULL;
149     }
150     // Filter out unknown settings, and fill in defaults for missing settings.
151     $default_settings = $this->getDefaultSettings($configuration['type']);
152     $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings;
153
154     return $configuration;
155   }
156
157   /**
158    * Returns an array of widget type options for a field type.
159    *
160    * @param string|null $field_type
161    *   (optional) The name of a field type, or NULL to retrieve all widget
162    *   options. Defaults to NULL.
163    *
164    * @return array
165    *   If no field type is provided, returns a nested array of all widget types,
166    *   keyed by field type human name.
167    */
168   public function getOptions($field_type = NULL) {
169     if (!isset($this->widgetOptions)) {
170       $options = [];
171       $field_types = $this->fieldTypeManager->getDefinitions();
172       $widget_types = $this->getDefinitions();
173       uasort($widget_types, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
174       foreach ($widget_types as $name => $widget_type) {
175         foreach ($widget_type['field_types'] as $widget_field_type) {
176           // Check that the field type exists.
177           if (isset($field_types[$widget_field_type])) {
178             $options[$widget_field_type][$name] = $widget_type['label'];
179           }
180         }
181       }
182       $this->widgetOptions = $options;
183     }
184     if (isset($field_type)) {
185       return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : [];
186     }
187
188     return $this->widgetOptions;
189   }
190
191   /**
192    * Returns the default settings of a field widget.
193    *
194    * @param string $type
195    *   A field widget type name.
196    *
197    * @return array
198    *   The widget type's default settings, as provided by the plugin
199    *   definition, or an empty array if type or settings are undefined.
200    */
201   public function getDefaultSettings($type) {
202     $plugin_definition = $this->getDefinition($type, FALSE);
203     if (!empty($plugin_definition['class'])) {
204       $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
205       return $plugin_class::defaultSettings();
206     }
207
208     return [];
209   }
210
211 }