PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/lib/Drupal/Core/Field/FormatterPluginManager.php

https://gitlab.com/mohamed_hussein/prodt
PHP | 210 lines | 88 code | 20 blank | 102 comment | 13 complexity | 8d4010e4ed42707623a710419b909659 MD5 | raw file
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Component\Plugin\Factory\DefaultFactory;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\Plugin\DefaultPluginManager;
  7. /**
  8. * Plugin type manager for field formatters.
  9. *
  10. * @ingroup field_formatter
  11. */
  12. class FormatterPluginManager extends DefaultPluginManager {
  13. /**
  14. * An array of formatter options for each field type.
  15. *
  16. * @var array
  17. */
  18. protected $formatterOptions;
  19. /**
  20. * The field type manager to define field.
  21. *
  22. * @var \Drupal\Core\Field\FieldTypePluginManagerInterface
  23. */
  24. protected $fieldTypeManager;
  25. /**
  26. * Constructs a FormatterPluginManager object.
  27. *
  28. * @param \Traversable $namespaces
  29. * An object that implements \Traversable which contains the root paths
  30. * keyed by the corresponding namespace to look for plugin implementations.
  31. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  32. * Cache backend instance to use.
  33. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  34. * The module handler.
  35. * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
  36. * The 'field type' plugin manager.
  37. */
  38. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, FieldTypePluginManagerInterface $field_type_manager) {
  39. parent::__construct('Plugin/Field/FieldFormatter', $namespaces, $module_handler, 'Drupal\Core\Field\FormatterInterface', 'Drupal\Core\Field\Annotation\FieldFormatter');
  40. $this->setCacheBackend($cache_backend, 'field_formatter_types_plugins');
  41. $this->alterInfo('field_formatter_info');
  42. $this->fieldTypeManager = $field_type_manager;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function createInstance($plugin_id, array $configuration = []) {
  48. $plugin_definition = $this->getDefinition($plugin_id);
  49. $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
  50. // @todo This is copied from \Drupal\Core\Plugin\Factory\ContainerFactory.
  51. // Find a way to restore sanity to
  52. // \Drupal\Core\Field\FormatterBase::__construct().
  53. // If the plugin provides a factory method, pass the container to it.
  54. if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
  55. return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
  56. }
  57. return new $plugin_class($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings']);
  58. }
  59. /**
  60. * Overrides PluginManagerBase::getInstance().
  61. *
  62. * @param array $options
  63. * An array with the following key/value pairs:
  64. * - field_definition: (FieldDefinitionInterface) The field definition.
  65. * - view_mode: (string) The view mode.
  66. * - prepare: (bool, optional) Whether default values should get merged in
  67. * the 'configuration' array. Defaults to TRUE.
  68. * - configuration: (array) the configuration for the formatter. The
  69. * following key value pairs are allowed, and are all optional if
  70. * 'prepare' is TRUE:
  71. * - label: (string) Position of the label. The default 'field' theme
  72. * implementation supports the values 'inline', 'above' and 'hidden'.
  73. * Defaults to 'above'.
  74. * - type: (string) The formatter to use. Defaults to the
  75. * 'default_formatter' for the field type, The default formatter will
  76. * also be used if the requested formatter is not available.
  77. * - settings: (array) Settings specific to the formatter. Each setting
  78. * defaults to the default value specified in the formatter definition.
  79. * - third_party_settings: (array) Settings provided by other extensions
  80. * through hook_field_formatter_third_party_settings_form().
  81. *
  82. * @return \Drupal\Core\Field\FormatterInterface|null
  83. * A formatter object or NULL when plugin is not found.
  84. */
  85. public function getInstance(array $options) {
  86. $configuration = $options['configuration'];
  87. $field_definition = $options['field_definition'];
  88. $field_type = $field_definition->getType();
  89. // Fill in default configuration if needed.
  90. if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
  91. $configuration = $this->prepareConfiguration($field_type, $configuration);
  92. }
  93. $plugin_id = $configuration['type'];
  94. // Switch back to default formatter if either:
  95. // - the configuration does not specify a formatter class
  96. // - the field type is not allowed for the formatter
  97. // - the formatter is not applicable to the field definition.
  98. $definition = $this->getDefinition($configuration['type'], FALSE);
  99. if (!isset($definition['class']) || !in_array($field_type, $definition['field_types']) || !$definition['class']::isApplicable($field_definition)) {
  100. // Grab the default widget for the field type.
  101. $field_type_definition = $this->fieldTypeManager->getDefinition($field_type);
  102. if (empty($field_type_definition['default_formatter'])) {
  103. return NULL;
  104. }
  105. $plugin_id = $field_type_definition['default_formatter'];
  106. }
  107. $configuration += [
  108. 'field_definition' => $field_definition,
  109. 'view_mode' => $options['view_mode'],
  110. ];
  111. return $this->createInstance($plugin_id, $configuration);
  112. }
  113. /**
  114. * Merges default values for formatter configuration.
  115. *
  116. * @param string $field_type
  117. * The field type.
  118. * @param array $configuration
  119. * An array of formatter configuration.
  120. *
  121. * @return array
  122. * The display properties with defaults added.
  123. */
  124. public function prepareConfiguration($field_type, array $configuration) {
  125. // Fill in defaults for missing properties.
  126. $configuration += [
  127. 'label' => 'above',
  128. 'settings' => [],
  129. 'third_party_settings' => [],
  130. ];
  131. // If no formatter is specified, use the default formatter.
  132. if (!isset($configuration['type'])) {
  133. $field_type = $this->fieldTypeManager->getDefinition($field_type);
  134. $configuration['type'] = $field_type['default_formatter'];
  135. }
  136. // Filter out unknown settings, and fill in defaults for missing settings.
  137. $default_settings = $this->getDefaultSettings($configuration['type']);
  138. $configuration['settings'] = array_intersect_key($configuration['settings'], $default_settings) + $default_settings;
  139. return $configuration;
  140. }
  141. /**
  142. * Returns an array of formatter options for a field type.
  143. *
  144. * @param string|null $field_type
  145. * (optional) The name of a field type, or NULL to retrieve all formatters.
  146. *
  147. * @return array
  148. * If no field type is provided, returns a nested array of all formatters,
  149. * keyed by field type.
  150. */
  151. public function getOptions($field_type = NULL) {
  152. if (!isset($this->formatterOptions)) {
  153. $options = [];
  154. $field_types = $this->fieldTypeManager->getDefinitions();
  155. $formatter_types = $this->getDefinitions();
  156. uasort($formatter_types, ['Drupal\Component\Utility\SortArray', 'sortByWeightElement']);
  157. foreach ($formatter_types as $name => $formatter_type) {
  158. foreach ($formatter_type['field_types'] as $formatter_field_type) {
  159. // Check that the field type exists.
  160. if (isset($field_types[$formatter_field_type])) {
  161. $options[$formatter_field_type][$name] = $formatter_type['label'];
  162. }
  163. }
  164. }
  165. $this->formatterOptions = $options;
  166. }
  167. if ($field_type) {
  168. return !empty($this->formatterOptions[$field_type]) ? $this->formatterOptions[$field_type] : [];
  169. }
  170. return $this->formatterOptions;
  171. }
  172. /**
  173. * Returns the default settings of a field formatter.
  174. *
  175. * @param string $type
  176. * A field formatter type name.
  177. *
  178. * @return array
  179. * The formatter type's default settings, as provided by the plugin
  180. * definition, or an empty array if type or settings are undefined.
  181. */
  182. public function getDefaultSettings($type) {
  183. $plugin_definition = $this->getDefinition($type, FALSE);
  184. if (!empty($plugin_definition['class'])) {
  185. $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
  186. return $plugin_class::defaultSettings();
  187. }
  188. return [];
  189. }
  190. }