WidgetPluginManager.php 7.8 KB

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