WidgetPluginManager.php 7.92 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\Core\Field\WidgetPluginManager.
6 7
 */

8
namespace Drupal\Core\Field;
9

10
use Drupal\Component\Plugin\Factory\DefaultFactory;
11 12 13
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
14 15 16

/**
 * Plugin type manager for field widgets.
17 18
 *
 * @ingroup field_widget
19
 */
20
class WidgetPluginManager extends DefaultPluginManager {
21

22 23 24
  /**
   * The field type manager to define field.
   *
25
   * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
26 27 28
   */
  protected $fieldTypeManager;

29 30 31 32 33 34 35
  /**
   * An array of widget options for each field type.
   *
   * @var array
   */
  protected $widgetOptions;

36 37
  /**
   * Constructs a WidgetPluginManager object.
38
   *
39 40
   * @param \Traversable $namespaces
   *   An object that implements \Traversable which contains the root paths
41 42 43 44 45
   *   keyed by the corresponding namespace to look for plugin implementations.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
   *   Cache backend instance to use.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
46
   * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
47
   *   The 'field type' plugin manager.
48
   */
49
  public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
50
    parent::__construct('Plugin/Field/FieldWidget', $namespaces, $module_handler, 'Drupal\Core\Field\WidgetInterface', 'Drupal\Core\Field\Annotation\FieldWidget');
51

52
    $this->setCacheBackend($cache_backend, 'field_widget_types_plugins');
53
    $this->alterInfo('field_widget_info');
54
    $this->fieldTypeManager = $field_type_manager;
55 56 57
  }

  /**
58 59 60 61
   * Overrides PluginManagerBase::getInstance().
   *
   * @param array $options
   *   An array with the following key/value pairs:
62
   *   - field_definition: (FieldDefinitionInterface) The field definition.
63 64 65 66 67 68 69
   *   - form_mode: (string) The form mode.
   *   - prepare: (bool, optional) Whether default values should get merged in
   *     the 'configuration' array. Defaults to TRUE.
   *   - configuration: (array) the configuration for the widget. The
   *     following key value pairs are allowed, and are all optional if
   *     'prepare' is TRUE:
   *     - type: (string) The widget to use. Defaults to the
70 71
   *       'default_widget' for the field type. The default widget will also be
   *       used if the requested widget is not available.
72 73
   *     - settings: (array) Settings specific to the widget. Each setting
   *       defaults to the default value specified in the widget definition.
74 75
   *     - third_party_settings: (array) Settings provided by other extensions
   *       through hook_field_formatter_third_party_settings_form().
76
   *
77 78
   * @return \Drupal\Core\Field\WidgetInterface|null
   *   A Widget object or NULL when plugin is not found.
79 80
   */
  public function getInstance(array $options) {
81 82 83 84 85 86
    // Fill in defaults for missing properties.
    $options += array(
      'configuration' => array(),
      'prepare' => TRUE,
    );

87
    $configuration = $options['configuration'];
88
    $field_definition = $options['field_definition'];
89
    $field_type = $field_definition->getType();
90

91
    // Fill in default configuration if needed.
92
    if ($options['prepare']) {
93
      $configuration = $this->prepareConfiguration($field_type, $configuration);
94 95 96 97
    }

    $plugin_id = $configuration['type'];

98
    // Switch back to default widget if either:
99 100 101
    // - the configuration does not specify a widget class
    // - the field type is not allowed for the widget
    // - the widget is not applicable to the field definition.
102
    $definition = $this->getDefinition($configuration['type'], FALSE);
103
    if (!isset($definition['class']) || !in_array($field_type, $definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
104
      // Grab the default widget for the field type.
105
      $field_type_definition = $this->fieldTypeManager->getDefinition($field_type);
106 107 108
      if (empty($field_type_definition['default_widget'])) {
        return NULL;
      }
109
      $plugin_id = $field_type_definition['default_widget'];
110 111
    }

112
    $configuration += array(
113
      'field_definition' => $field_definition,
114
    );
115
    return $this->createInstance($plugin_id, $configuration);
116 117
  }

118 119 120 121
  /**
   * {@inheritdoc}
   */
  public function createInstance($plugin_id, array $configuration = array()) {
122
    $plugin_definition = $this->getDefinition($plugin_id);
123 124 125 126 127 128 129
    $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);

    // If the plugin provides a factory method, pass the container to it.
    if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
      return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
    }

130
    return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['third_party_settings']);
131 132 133
  }


134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  /**
   * Merges default values for widget configuration.
   *
   * @param string $field_type
   *   The field type.
   * @param array $configuration
   *   An array of widget configuration.
   *
   * @return array
   *   The display properties with defaults added.
   */
  public function prepareConfiguration($field_type, array $configuration) {
    // Fill in defaults for missing properties.
    $configuration += array(
      'settings' => array(),
149
      'third_party_settings' => array(),
150 151 152
    );
    // If no widget is specified, use the default widget.
    if (!isset($configuration['type'])) {
153
      $field_type = $this->fieldTypeManager->getDefinition($field_type);
154
      $configuration['type'] = isset($field_type['default_widget']) ? $field_type['default_widget'] : NULL;
155
    }
156 157 158
    // Filter out unknown settings, and fill in defaults for missing settings.
    $default_settings = $this->getDefaultSettings($configuration['type']);
    $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings;
159 160 161

    return $configuration;
  }
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176

  /**
   * Returns an array of widget type options for a field type.
   *
   * @param string|null $field_type
   *   (optional) The name of a field type, or NULL to retrieve all widget
   *   options. Defaults to NULL.
   *
   * @return array
   *   If no field type is provided, returns a nested array of all widget types,
   *   keyed by field type human name.
   */
  public function getOptions($field_type = NULL) {
    if (!isset($this->widgetOptions)) {
      $options = array();
177
      $field_types = $this->fieldTypeManager->getDefinitions();
178
      $widget_types = $this->getDefinitions();
179
      uasort($widget_types, array('Drupal\Component\Utility\SortArray', 'sortByWeightElement'));
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
      foreach ($widget_types as $name => $widget_type) {
        foreach ($widget_type['field_types'] as $widget_field_type) {
          // Check that the field type exists.
          if (isset($field_types[$widget_field_type])) {
            $options[$widget_field_type][$name] = $widget_type['label'];
          }
        }
      }
      $this->widgetOptions = $options;
    }
    if (isset($field_type)) {
      return !empty($this->widgetOptions[$field_type]) ? $this->widgetOptions[$field_type] : array();
    }

    return $this->widgetOptions;
  }

197 198 199 200 201 202 203 204 205 206 207
  /**
   * Returns the default settings of a field widget.
   *
   * @param string $type
   *   A field widget type name.
   *
   * @return array
   *   The widget type's default settings, as provided by the plugin
   *   definition, or an empty array if type or settings are undefined.
   */
  public function getDefaultSettings($type) {
208
    $plugin_definition = $this->getDefinition($type, FALSE);
209 210 211 212 213 214
    if (!empty($plugin_definition['class'])) {
      $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
      return $plugin_class::defaultSettings();
    }

    return array();
215 216
  }

217
}