3 namespace Drupal\Core\Field;
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;
11 * Plugin type manager for field widgets.
13 * @ingroup field_widget
15 class WidgetPluginManager extends DefaultPluginManager {
18 * The field type manager to define field.
20 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
22 protected $fieldTypeManager;
25 * An array of widget options for each field type.
29 protected $widgetOptions;
32 * Constructs a WidgetPluginManager object.
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
41 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
42 * The 'field type' plugin manager.
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');
47 $this->setCacheBackend($cache_backend, 'field_widget_types_plugins');
48 $this->alterInfo('field_widget_info');
49 $this->fieldTypeManager = $field_type_manager;
53 * Overrides PluginManagerBase::getInstance().
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
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().
72 * @return \Drupal\Core\Field\WidgetInterface|null
73 * A Widget object or NULL when plugin is not found.
75 public function getInstance(array $options) {
76 // Fill in defaults for missing properties.
78 'configuration' => [],
82 $configuration = $options['configuration'];
83 $field_definition = $options['field_definition'];
84 $field_type = $field_definition->getType();
86 // Fill in default configuration if needed.
87 if ($options['prepare']) {
88 $configuration = $this->prepareConfiguration($field_type, $configuration);
91 $plugin_id = $configuration['type'];
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'])) {
104 $plugin_id = $field_type_definition['default_widget'];
108 'field_definition' => $field_definition,
110 return $this->createInstance($plugin_id, $configuration);
116 public function createInstance($plugin_id, array $configuration = []) {
117 $plugin_definition = $this->getDefinition($plugin_id);
118 $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
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);
125 return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings']);
129 * Merges default values for widget configuration.
131 * @param string $field_type
133 * @param array $configuration
134 * An array of widget configuration.
137 * The display properties with defaults added.
139 public function prepareConfiguration($field_type, array $configuration) {
140 // Fill in defaults for missing properties.
143 'third_party_settings' => [],
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;
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;
154 return $configuration;
158 * Returns an array of widget type options for a field type.
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.
165 * If no field type is provided, returns a nested array of all widget types,
166 * keyed by field type human name.
168 public function getOptions($field_type = NULL) {
169 if (!isset($this->widgetOptions)) {
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'];
182 $this->widgetOptions = $options;
184 if (isset($field_type)) {
185 return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : [];
188 return $this->widgetOptions;
192 * Returns the default settings of a field widget.
194 * @param string $type
195 * A field widget type name.
198 * The widget type's default settings, as provided by the plugin
199 * definition, or an empty array if type or settings are undefined.
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();