FieldGroupFormatterPluginManager.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Drupal\field_group;
  3. use Drupal\Component\Plugin\Factory\DefaultFactory;
  4. use Drupal\Core\Cache\CacheBackendInterface;
  5. use Drupal\Core\Plugin\DefaultPluginManager;
  6. use Drupal\Core\Extension\ModuleHandlerInterface;
  7. /**
  8. * Plugin type manager for all fieldgroup formatters.
  9. */
  10. class FieldGroupFormatterPluginManager extends DefaultPluginManager {
  11. /**
  12. * Constructs a new FieldGroupFormatterPluginManager object.
  13. *
  14. * @param \Traversable $namespaces
  15. * An object that implements \Traversable which contains the root paths
  16. * keyed by the corresponding namespace to look for plugin implementations.
  17. * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
  18. * Cache backend instance to use.
  19. * @param \Drupal\Core\Language\LanguageManager $language_manager
  20. * The language manager.
  21. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  22. * The module handler to invoke the alter hook with.
  23. */
  24. public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
  25. parent::__construct('Plugin/field_group/FieldGroupFormatter', $namespaces, $module_handler, 'Drupal\field_group\FieldGroupFormatterInterface', 'Drupal\field_group\Annotation\FieldGroupFormatter');
  26. $this->alterInfo('field_group_formatter_info');
  27. $this->setCacheBackend($cache_backend, 'field_group_formatter_info');
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function createInstance($plugin_id, array $configuration = array()) {
  33. $plugin_definition = $this->getDefinition($plugin_id);
  34. $plugin_class = DefaultFactory::getPluginClass($plugin_id, $plugin_definition);
  35. // If the plugin provides a factory method, pass the container to it.
  36. if (is_subclass_of($plugin_class, 'Drupal\Core\Plugin\ContainerFactoryPluginInterface')) {
  37. return $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
  38. }
  39. return new $plugin_class($plugin_id, $plugin_definition, $configuration['group'], $configuration['settings'], $configuration['label']);
  40. }
  41. /**
  42. * Overrides PluginManagerBase::getInstance().
  43. *
  44. * @param array $options
  45. * An array with the following key/value pairs:
  46. * - format_type: The current format type.
  47. * - group: The current group.
  48. * - prepare: (bool, optional) Whether default values should get merged in
  49. * the 'configuration' array. Defaults to TRUE.
  50. * - configuration: (array) the configuration for the formatter. The
  51. * following key value pairs are allowed, and are all optional if
  52. * 'prepare' is TRUE:
  53. * - label: (string) Position of the label. The default 'field' theme
  54. * implementation supports the values 'inline', 'above' and 'hidden'.
  55. * Defaults to 'above'.
  56. * - settings: (array) Settings specific to the formatter. Each setting
  57. * defaults to the default value specified in the formatter definition.
  58. *
  59. * @return \Drupal\field_group\FieldGroupFormatterInterface|null
  60. * A formatter object or NULL when plugin is not found.
  61. */
  62. public function getInstance(array $options) {
  63. $configuration = $options['configuration'];
  64. $format_type = $options['format_type'];
  65. $context = $options['group']->context;
  66. // Fill in default configuration if needed.
  67. if (!isset($options['prepare']) || $options['prepare'] == TRUE) {
  68. $configuration = $this->prepareConfiguration($format_type, $context, $configuration);
  69. }
  70. $plugin_id = $format_type;
  71. // Validate if plugin exists and it's allowed for current context.
  72. $definition = $this->getDefinition($format_type, FALSE);
  73. if (!isset($definition['class']) || !in_array($context, $definition['supported_contexts'])) {
  74. return NULL;
  75. }
  76. $configuration += array(
  77. 'group' => $options['group'],
  78. );
  79. return $this->createInstance($plugin_id, $configuration);
  80. }
  81. /**
  82. * Merges default values for formatter configuration.
  83. *
  84. * @param string $format_type
  85. * The format type
  86. * @param string $context
  87. * The context to prepare configuration for.
  88. * @param array $properties
  89. * An array of formatter configuration.
  90. *
  91. * @return array
  92. * The display properties with defaults added.
  93. */
  94. public function prepareConfiguration($format_type, $context, array $configuration) {
  95. // Fill in defaults for missing properties.
  96. $configuration += array(
  97. 'label' => '',
  98. 'settings' => array(),
  99. );
  100. // Fill in default settings values for the formatter.
  101. $configuration['settings'] += $this->getDefaultSettings($format_type, $context);
  102. return $configuration;
  103. }
  104. /**
  105. * Returns the default settings of a field_group formatter.
  106. *
  107. * @param string $type
  108. * A formatter type name.
  109. * @param string $context
  110. * The context to get default values for.
  111. *
  112. * @return array
  113. * The formatter type's default settings, as provided by the plugin
  114. * definition, or an empty array if type or settings are undefined.
  115. */
  116. public function getDefaultSettings($type, $context) {
  117. $plugin_definition = $this->getDefinition($type, FALSE);
  118. if (!empty($plugin_definition['class'])) {
  119. $plugin_class = DefaultFactory::getPluginClass($type, $plugin_definition);
  120. return $plugin_class::defaultContextSettings($context);
  121. }
  122. return array();
  123. }
  124. }