3 namespace Drupal\Core\Field;
5 use Drupal\Component\Plugin\Factory\DefaultFactory;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
11 * Plugin type manager for field formatters.
13 * @ingroup field_formatter
15 class FormatterPluginManager extends DefaultPluginManager {
18 * An array of formatter options for each field type.
22 protected $formatterOptions;
25 * The field type manager to define field.
27 * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
29 protected $fieldTypeManager;
32 * Constructs a FormatterPluginManager object.
34 * @param \Traversable $namespaces
35 * An object that implements \Traversable which contains the root paths
36 * keyed by the corresponding namespace to look for plugin implementations.
37 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
38 * Cache backend instance to use.
39 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
41 * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
42 * The 'field type' plugin manager.
44 public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
45 parent::__construct('Plugin/Field/FieldFormatter', $namespaces, $module_handler, 'Drupal\Core\Field\FormatterInterface', 'Drupal\Core\Field\Annotation\FieldFormatter');
47 $this->setCacheBackend($cache_backend, 'field_formatter_types_plugins');
48 $this->alterInfo('field_formatter_info');
49 $this->fieldTypeManager = $field_type_manager;
55 public function createInstance($plugin_id, array $configuration = []) {
56 $plugin_definition = $this->getDefinition($plugin_id);
57 $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
59 // @todo This is copied from \Drupal\Core\Plugin\Factory\ContainerFactory.
60 // Find a way to restore sanity to
61 // \Drupal\Core\Field\FormatterBase::__construct().
62 // If the plugin provides a factory method, pass the container to it.
63 if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
64 return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
67 return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings']);
71 * Overrides PluginManagerBase::getInstance().
73 * @param array $options
74 * An array with the following key/value pairs:
75 * - field_definition: (FieldDefinitionInterface) The field definition.
76 * - view_mode: (string) The view mode.
77 * - prepare: (bool, optional) Whether default values should get merged in
78 * the 'configuration' array. Defaults to TRUE.
79 * - configuration: (array) the configuration for the formatter. The
80 * following key value pairs are allowed, and are all optional if
82 * - label: (string) Position of the label. The default 'field' theme
83 * implementation supports the values 'inline', 'above' and 'hidden'.
84 * Defaults to 'above'.
85 * - type: (string) The formatter to use. Defaults to the
86 * 'default_formatter' for the field type, The default formatter will
87 * also be used if the requested formatter is not available.
88 * - settings: (array) Settings specific to the formatter. Each setting
89 * defaults to the default value specified in the formatter definition.
90 * - third_party_settings: (array) Settings provided by other extensions
91 * through hook_field_formatter_third_party_settings_form().
93 * @return \Drupal\Core\Field\FormatterInterface|null
94 * A formatter object or NULL when plugin is not found.
96 public function getInstance(array $options) {
97 $configuration = $options['configuration'];
98 $field_definition = $options['field_definition'];
99 $field_type = $field_definition->getType();
101 // Fill in default configuration if needed.
102 if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
103 $configuration = $this->prepareConfiguration($field_type, $configuration);
106 $plugin_id = $configuration['type'];
108 // Switch back to default formatter if either:
109 // - the configuration does not specify a formatter class
110 // - the field type is not allowed for the formatter
111 // - the formatter is not applicable to the field definition.
112 $definition = $this->getDefinition($configuration['type'], FALSE);
113 if (!isset($definition['class']) || !in_array($field_type, $definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
114 // Grab the default widget for the field type.
115 $field_type_definition = $this->fieldTypeManager->getDefinition($field_type);
116 if (empty($field_type_definition['default_formatter'])) {
119 $plugin_id = $field_type_definition['default_formatter'];
123 'field_definition' => $field_definition,
124 'view_mode' => $options['view_mode'],
126 return $this->createInstance($plugin_id, $configuration);
130 * Merges default values for formatter configuration.
132 * @param string $field_type
134 * @param array $configuration
135 * An array of formatter configuration.
138 * The display properties with defaults added.
140 public function prepareConfiguration($field_type, array $configuration) {
141 // Fill in defaults for missing properties.
145 'third_party_settings' => [],
147 // If no formatter is specified, use the default formatter.
148 if (!isset($configuration['type'])) {
149 $field_type = $this->fieldTypeManager->getDefinition($field_type);
150 $configuration['type'] = $field_type['default_formatter'];
152 // Filter out unknown settings, and fill in defaults for missing settings.
153 $default_settings = $this->getDefaultSettings($configuration['type']);
154 $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings;
156 return $configuration;
160 * Returns an array of formatter options for a field type.
162 * @param string|null $field_type
163 * (optional) The name of a field type, or NULL to retrieve all formatters.
166 * If no field type is provided, returns a nested array of all formatters,
167 * keyed by field type.
169 public function getOptions($field_type = NULL) {
170 if (!isset($this->formatterOptions)) {
172 $field_types = $this->fieldTypeManager->getDefinitions();
173 $formatter_types = $this->getDefinitions();
174 uasort($formatter_types, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
175 foreach ($formatter_types as $name => $formatter_type) {
176 foreach ($formatter_type['field_types'] as $formatter_field_type) {
177 // Check that the field type exists.
178 if (isset($field_types[$formatter_field_type])) {
179 $options[$formatter_field_type][$name] = $formatter_type['label'];
183 $this->formatterOptions = $options;
186 return !empty($this->formatterOptions[$field_type]) ? $this->formatterOptions[$field_type] : [];
188 return $this->formatterOptions;
192 * Returns the default settings of a field formatter.
194 * @param string $type
195 * A field formatter type name.
198 * The formatter type's default settings, as provided by the plugin
199 * definition, or an empty array if type or settings are undefined.
201 public function getDefaultSettings($type) {
202 $plugin_definition = $this->getDefinition($type, FALSE);
203 if (!empty($plugin_definition['class'])) {
204 $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
205 return $plugin_class::defaultSettings();