public function FormatterPluginManager::getInstance

Overrides PluginManagerBase::getInstance().

Parameters

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

  • instance: (FieldInstance) The field instance.
  • 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, specified in hook_field_info(). 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.

Return value

\Drupal\field\Plugin\Type\Formatter\FormatterInterface A formatter object.

Overrides PluginManagerBase::getInstance

File

drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php, line 77
Definition of Drupal\field\Plugin\Type\Formatter\FormatterPluginManager..

Class

FormatterPluginManager
Plugin type manager for field formatters.

Namespace

Drupal\field\Plugin\Type\Formatter

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 formatter 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_formatter'];
  }
  $configuration += array(
    'instance' => $instance,
    'view_mode' => $options['view_mode'],
  );
  return $this
    ->createInstance($plugin_id, $configuration);
}