Chris@0: setCacheBackend($cache_backend, 'field_widget_types_plugins'); Chris@0: $this->alterInfo('field_widget_info'); Chris@0: $this->fieldTypeManager = $field_type_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Overrides PluginManagerBase::getInstance(). Chris@0: * Chris@0: * @param array $options Chris@0: * An array with the following key/value pairs: Chris@0: * - field_definition: (FieldDefinitionInterface) The field definition. Chris@0: * - form_mode: (string) The form mode. Chris@0: * - prepare: (bool, optional) Whether default values should get merged in Chris@0: * the 'configuration' array. Defaults to TRUE. Chris@0: * - configuration: (array) the configuration for the widget. The Chris@0: * following key value pairs are allowed, and are all optional if Chris@0: * 'prepare' is TRUE: Chris@0: * - type: (string) The widget to use. Defaults to the Chris@0: * 'default_widget' for the field type. The default widget will also be Chris@0: * used if the requested widget is not available. Chris@0: * - settings: (array) Settings specific to the widget. Each setting Chris@0: * defaults to the default value specified in the widget definition. Chris@0: * - third_party_settings: (array) Settings provided by other extensions Chris@0: * through hook_field_formatter_third_party_settings_form(). Chris@0: * Chris@0: * @return \Drupal\Core\Field\WidgetInterface|null Chris@0: * A Widget object or NULL when plugin is not found. Chris@0: */ Chris@0: public function getInstance(array $options) { Chris@0: // Fill in defaults for missing properties. Chris@0: $options += [ Chris@0: 'configuration' => [], Chris@0: 'prepare' => TRUE, Chris@0: ]; Chris@0: Chris@0: $configuration = $options['configuration']; Chris@0: $field_definition = $options['field_definition']; Chris@0: $field_type = $field_definition->getType(); Chris@0: Chris@0: // Fill in default configuration if needed. Chris@0: if ($options['prepare']) { Chris@0: $configuration = $this->prepareConfiguration($field_type, $configuration); Chris@0: } Chris@0: Chris@0: $plugin_id = $configuration['type']; Chris@0: Chris@0: // Switch back to default widget if either: Chris@0: // - the configuration does not specify a widget class Chris@0: // - the field type is not allowed for the widget Chris@0: // - the widget is not applicable to the field definition. Chris@0: $definition = $this->getDefinition($configuration['type'], FALSE); Chris@0: if (!isset($definition['class']) || !in_array($field_type, $definition['field_types']) || !$definition['class']::isApplicable($field_definition)) { Chris@0: // Grab the default widget for the field type. Chris@0: $field_type_definition = $this->fieldTypeManager->getDefinition($field_type); Chris@0: if (empty($field_type_definition['default_widget'])) { Chris@0: return NULL; Chris@0: } Chris@0: $plugin_id = $field_type_definition['default_widget']; Chris@0: } Chris@0: Chris@0: $configuration += [ Chris@0: 'field_definition' => $field_definition, Chris@0: ]; Chris@0: return $this->createInstance($plugin_id, $configuration); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function createInstance($plugin_id, array $configuration = []) { Chris@0: $plugin_definition = $this->getDefinition($plugin_id); Chris@0: $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition); Chris@0: Chris@0: // If the plugin provides a factory method, pass the container to it. Chris@0: if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) { Chris@0: return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition); Chris@0: } Chris@0: Chris@0: return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges default values for widget configuration. Chris@0: * Chris@0: * @param string $field_type Chris@0: * The field type. Chris@0: * @param array $configuration Chris@0: * An array of widget configuration. Chris@0: * Chris@0: * @return array Chris@0: * The display properties with defaults added. Chris@0: */ Chris@0: public function prepareConfiguration($field_type, array $configuration) { Chris@0: // Fill in defaults for missing properties. Chris@0: $configuration += [ Chris@0: 'settings' => [], Chris@0: 'third_party_settings' => [], Chris@0: ]; Chris@0: // If no widget is specified, use the default widget. Chris@0: if (!isset($configuration['type'])) { Chris@0: $field_type = $this->fieldTypeManager->getDefinition($field_type); Chris@0: $configuration['type'] = isset($field_type['default_widget']) ? $field_type['default_widget'] : NULL; Chris@0: } Chris@0: // Filter out unknown settings, and fill in defaults for missing settings. Chris@0: $default_settings = $this->getDefaultSettings($configuration['type']); Chris@0: $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings; Chris@0: Chris@0: return $configuration; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of widget type options for a field type. Chris@0: * Chris@0: * @param string|null $field_type Chris@0: * (optional) The name of a field type, or NULL to retrieve all widget Chris@0: * options. Defaults to NULL. Chris@0: * Chris@0: * @return array Chris@0: * If no field type is provided, returns a nested array of all widget types, Chris@0: * keyed by field type human name. Chris@0: */ Chris@0: public function getOptions($field_type = NULL) { Chris@0: if (!isset($this->widgetOptions)) { Chris@0: $options = []; Chris@0: $field_types = $this->fieldTypeManager->getDefinitions(); Chris@0: $widget_types = $this->getDefinitions(); Chris@0: uasort($widget_types, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']); Chris@0: foreach ($widget_types as $name => $widget_type) { Chris@0: foreach ($widget_type['field_types'] as $widget_field_type) { Chris@0: // Check that the field type exists. Chris@0: if (isset($field_types[$widget_field_type])) { Chris@0: $options[$widget_field_type][$name] = $widget_type['label']; Chris@0: } Chris@0: } Chris@0: } Chris@0: $this->widgetOptions = $options; Chris@0: } Chris@0: if (isset($field_type)) { Chris@0: return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : []; Chris@0: } Chris@0: Chris@0: return $this->widgetOptions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the default settings of a field widget. Chris@0: * Chris@0: * @param string $type Chris@0: * A field widget type name. Chris@0: * Chris@0: * @return array Chris@0: * The widget type's default settings, as provided by the plugin Chris@0: * definition, or an empty array if type or settings are undefined. Chris@0: */ Chris@0: public function getDefaultSettings($type) { Chris@0: $plugin_definition = $this->getDefinition($type, FALSE); Chris@0: if (!empty($plugin_definition['class'])) { Chris@0: $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition); Chris@0: return $plugin_class::defaultSettings(); Chris@0: } Chris@0: Chris@0: return []; Chris@0: } Chris@0: Chris@0: }