function FormatterPluginManager::getInstance

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

Overrides PluginManagerBase::getInstance().

Parameters

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

  • field_definition: (FieldDefinitionInterface) The field definition.
  • view_mode: (string) The view mode.
  • prepare: (bool, optional) Whether default values should get merged in the 'configuration' array. Defaults to TRUE.
  • configuration: (array) the configuration for the formatter. The following key value pairs are allowed, and are all optional if 'prepare' is TRUE:

    • label: (string) Position of the label. The default 'field' theme implementation supports the values 'inline', 'above' and 'hidden'. Defaults to 'above'.
    • type: (string) The formatter to use. Defaults to the 'default_formatter' for the field type, The default formatter will also be used if the requested formatter is not available.
    • settings: (array) Settings specific to the formatter. Each setting defaults to the default value specified in the formatter definition.
    • third_party_settings: (array) Settings provided by other extensions through hook_field_formatter_third_party_settings_form().

Return value

\Drupal\Core\Field\FormatterInterface|null A formatter object or NULL when plugin is not found.

Overrides PluginManagerBase::getInstance

File

core/lib/Drupal/Core/Field/FormatterPluginManager.php, line 96

Class

FormatterPluginManager
Plugin type manager for field formatters.

Namespace

Drupal\Core\Field

Code

public function getInstance(array $options) {
    $configuration = $options['configuration'];
    $field_definition = $options['field_definition'];
    $field_type = $field_definition->getType();
    // 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 formatter if either:
    // - the configuration does not specify a formatter class
    // - the field type is not allowed for the formatter
    // - the formatter 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_formatter'])) {
            return NULL;
        }
        $plugin_id = $field_type_definition['default_formatter'];
    }
    $configuration += [
        'field_definition' => $field_definition,
        'view_mode' => $options['view_mode'],
    ];
    return $this->createInstance($plugin_id, $configuration);
}

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