function WidgetPluginManager::getInstance

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/WidgetPluginManager.php \Drupal\Core\Field\WidgetPluginManager::getInstance()
  2. 10 core/lib/Drupal/Core/Field/WidgetPluginManager.php \Drupal\Core\Field\WidgetPluginManager::getInstance()
  3. 11.x core/lib/Drupal/Core/Field/WidgetPluginManager.php \Drupal\Core\Field\WidgetPluginManager::getInstance()

Overrides PluginManagerBase::getInstance().

Parameters

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

  • field_definition: (FieldDefinitionInterface) The field definition.
  • 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. 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.
    • third_party_settings: (array) Settings provided by other extensions through hook_field_formatter_third_party_settings_form().

Return value

\Drupal\Core\Field\WidgetInterface|false A Widget object or FALSE when plugin is not found.

Overrides PluginManagerBase::getInstance

File

core/lib/Drupal/Core/Field/WidgetPluginManager.php, line 75

Class

WidgetPluginManager
Plugin type manager for field widgets.

Namespace

Drupal\Core\Field

Code

public function getInstance(array $options) {
    // Fill in defaults for missing properties.
    $options += [
        'configuration' => [],
        'prepare' => TRUE,
    ];
    $configuration = $options['configuration'];
    $field_definition = $options['field_definition'];
    $field_type = $field_definition->getType();
    // Fill in default configuration if needed.
    if ($options['prepare']) {
        $configuration = $this->prepareConfiguration($field_type, $configuration);
    }
    $plugin_id = $configuration['type'];
    // Switch back to default widget if either:
    // - 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.
    $definition = $this->getDefinition($configuration['type'], FALSE);
    if (!isset($definition['class']) || !in_array($field_type, $definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
        // Grab the default widget for the field type.
        $field_type_definition = $this->fieldTypeManager
            ->getDefinition($field_type);
        if (empty($field_type_definition['default_widget'])) {
            return FALSE;
        }
        $plugin_id = $field_type_definition['default_widget'];
    }
    $configuration += [
        'field_definition' => $field_definition,
    ];
    return $this->createInstance($plugin_id, $configuration) ?? FALSE;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.