public function WidgetPluginManager::getInstance

Overrides PluginManagerBase::getInstance().

Parameters

array $options: An array with the following key/value pairs:

  • instance: (FieldInstance) The field instance.
  • 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 'default_widget' for the field type, specified in hook_field_info(). The default widget will also be used if the requested widget is not available.
    • settings: (array) Settings specific to the widget. Each setting defaults to the default value specified in the widget definition.

Return value

\Drupal\field\Plugin\Type\Widget\WidgetInterface A Widget object.

Overrides PluginManagerBase::getInstance

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Widget/WidgetPluginManager.php, line 76
Definition of Drupal\field\Plugin\Type\Widget\WidgetPluginManager.

Class

WidgetPluginManager
Plugin type manager for field widgets.

Namespace

Drupal\field\Plugin\Type\Widget

Code

public function getInstance(array $options) {
  $configuration = $options['configuration'];
  $instance = $options['instance'];
  $field = field_info_field($instance['field_name']);

  // Fill in default configuration if needed.
  if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
    $configuration = $this
      ->prepareConfiguration($field['type'], $configuration);
  }
  $plugin_id = $configuration['type'];

  // Switch back to default widget if either:
  // - $type_info doesn't exist (the widget type is unknown),
  // - the field type is not allowed for the widget.
  $definition = $this
    ->getDefinition($configuration['type']);
  if (!isset($definition['class']) || !in_array($field['type'], $definition['field_types'])) {

    // Grab the default widget for the field type.
    $field_type_definition = field_info_field_types($field['type']);
    $plugin_id = $field_type_definition['default_widget'];
  }
  $configuration += array(
    'instance' => $instance,
  );
  return $this
    ->createInstance($plugin_id, $configuration);
}