Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Field / FormatterPluginManager.php
1 <?php
2
3 namespace Drupal\Core\Field;
4
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;
9
10 /**
11  * Plugin type manager for field formatters.
12  *
13  * @ingroup field_formatter
14  */
15 class FormatterPluginManager extends DefaultPluginManager {
16
17   /**
18    * An array of formatter options for each field type.
19    *
20    * @var array
21    */
22   protected $formatterOptions;
23
24   /**
25    * The field type manager to define field.
26    *
27    * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
28    */
29   protected $fieldTypeManager;
30
31   /**
32    * Constructs a FormatterPluginManager object.
33    *
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
40    *   The module handler.
41    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
42    *   The 'field type' plugin manager.
43    */
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');
46
47     $this->setCacheBackend($cache_backend, 'field_formatter_types_plugins');
48     $this->alterInfo('field_formatter_info');
49     $this->fieldTypeManager = $field_type_manager;
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   public function createInstance($plugin_id, array $configuration = []) {
56     $plugin_definition = $this->getDefinition($plugin_id);
57     $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
58
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);
65     }
66
67     return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings']);
68   }
69
70   /**
71    * Overrides PluginManagerBase::getInstance().
72    *
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
81    *     'prepare' is TRUE:
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().
92    *
93    * @return \Drupal\Core\Field\FormatterInterface|null
94    *   A formatter object or NULL when plugin is not found.
95    */
96   public function getInstance(array $options) {
97     $configuration = $options['configuration'];
98     $field_definition = $options['field_definition'];
99     $field_type = $field_definition->getType();
100
101     // Fill in default configuration if needed.
102     if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
103       $configuration = $this->prepareConfiguration($field_type, $configuration);
104     }
105
106     $plugin_id = $configuration['type'];
107
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'])) {
117         return NULL;
118       }
119       $plugin_id = $field_type_definition['default_formatter'];
120     }
121
122     $configuration += [
123       'field_definition' => $field_definition,
124       'view_mode' => $options['view_mode'],
125     ];
126     return $this->createInstance($plugin_id, $configuration);
127   }
128
129   /**
130    * Merges default values for formatter configuration.
131    *
132    * @param string $field_type
133    *   The field type.
134    * @param array $configuration
135    *   An array of formatter configuration.
136    *
137    * @return array
138    *   The display properties with defaults added.
139    */
140   public function prepareConfiguration($field_type, array $configuration) {
141     // Fill in defaults for missing properties.
142     $configuration += [
143       'label' => 'above',
144       'settings' => [],
145       'third_party_settings' => [],
146     ];
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'];
151     }
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;
155
156     return $configuration;
157   }
158
159   /**
160    * Returns an array of formatter options for a field type.
161    *
162    * @param string|null $field_type
163    *   (optional) The name of a field type, or NULL to retrieve all formatters.
164    *
165    * @return array
166    *   If no field type is provided, returns a nested array of all formatters,
167    *   keyed by field type.
168    */
169   public function getOptions($field_type = NULL) {
170     if (!isset($this->formatterOptions)) {
171       $options = [];
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'];
180           }
181         }
182       }
183       $this->formatterOptions = $options;
184     }
185     if ($field_type) {
186       return !empty($this->formatterOptions[$field_type]) ? $this->formatterOptions[$field_type] : [];
187     }
188     return $this->formatterOptions;
189   }
190
191   /**
192    * Returns the default settings of a field formatter.
193    *
194    * @param string $type
195    *   A field formatter type name.
196    *
197    * @return array
198    *   The formatter type's default settings, as provided by the plugin
199    *   definition, or an empty array if type or settings are undefined.
200    */
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();
206     }
207     return [];
208   }
209
210 }