CompositeConfigProvider.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Model\Wysiwyg;
  7. /**
  8. * Class CompositeConfigProvider loads required config by adapter specified in system configuration
  9. * General > Content Management >WYSIWYG Options > WYSIWYG Editor
  10. */
  11. class CompositeConfigProvider
  12. {
  13. /**
  14. * @var \Magento\Ui\Block\Wysiwyg\ActiveEditor
  15. */
  16. private $activeEditor;
  17. /**
  18. * List of variable config processors by adapter type
  19. *
  20. * @var array
  21. */
  22. private $variablePluginConfigProvider;
  23. /**
  24. * List of widget config processors by adapter type
  25. *
  26. * @var array
  27. */
  28. private $widgetPluginConfigProvider;
  29. /**
  30. * List of wysiwyg config postprocessors by adapter type
  31. *
  32. * @var array
  33. */
  34. private $wysiwygConfigPostProcessor;
  35. /**
  36. * Factory to create required processor object
  37. *
  38. * @var \Magento\Cms\Model\Wysiwyg\ConfigProviderFactory
  39. */
  40. private $configProviderFactory;
  41. /**
  42. * Current active editor path
  43. *
  44. * @var string
  45. */
  46. private $activeEditorPath;
  47. /**
  48. * List of gallery config processors by adapter type
  49. *
  50. * @var array
  51. */
  52. private $galleryConfigProvider;
  53. /**
  54. * @param \Magento\Ui\Block\Wysiwyg\ActiveEditor $activeEditor
  55. * @param ConfigProviderFactory $configProviderFactory
  56. * @param array $variablePluginConfigProvider
  57. * @param array $widgetPluginConfigProvider
  58. * @param array $galleryConfigProvider
  59. * @param array $wysiwygConfigPostProcessor
  60. */
  61. public function __construct(
  62. \Magento\Ui\Block\Wysiwyg\ActiveEditor $activeEditor,
  63. \Magento\Cms\Model\Wysiwyg\ConfigProviderFactory $configProviderFactory,
  64. array $variablePluginConfigProvider,
  65. array $widgetPluginConfigProvider,
  66. array $galleryConfigProvider,
  67. array $wysiwygConfigPostProcessor
  68. ) {
  69. $this->activeEditor = $activeEditor;
  70. $this->configProviderFactory = $configProviderFactory;
  71. $this->variablePluginConfigProvider = $variablePluginConfigProvider;
  72. $this->widgetPluginConfigProvider = $widgetPluginConfigProvider;
  73. $this->galleryConfigProvider = $galleryConfigProvider;
  74. $this->wysiwygConfigPostProcessor = $wysiwygConfigPostProcessor;
  75. }
  76. /**
  77. * Add config for variable plugin
  78. *
  79. * @param \Magento\Framework\DataObject $config
  80. * @return \Magento\Framework\DataObject
  81. */
  82. public function processVariableConfig($config)
  83. {
  84. return $this->updateConfig($config, $this->variablePluginConfigProvider);
  85. }
  86. /**
  87. * Add config for widget plugin
  88. *
  89. * @param \Magento\Framework\DataObject $config
  90. * @return \Magento\Framework\DataObject
  91. */
  92. public function processWidgetConfig($config)
  93. {
  94. return $this->updateConfig($config, $this->widgetPluginConfigProvider);
  95. }
  96. /**
  97. * Add config for gallery
  98. *
  99. * @param \Magento\Framework\DataObject $config
  100. * @return \Magento\Framework\DataObject
  101. */
  102. public function processGalleryConfig($config)
  103. {
  104. return $this->updateConfig($config, $this->galleryConfigProvider);
  105. }
  106. /**
  107. * Update wysiwyg config with data required for adapter
  108. *
  109. * @param \Magento\Framework\DataObject $config
  110. * @return \Magento\Framework\DataObject
  111. */
  112. public function processWysiwygConfig($config)
  113. {
  114. return $this->updateConfig($config, $this->wysiwygConfigPostProcessor);
  115. }
  116. /**
  117. * Returns active editor path
  118. *
  119. * @param \Magento\Framework\DataObject $config
  120. * @return string
  121. */
  122. private function getActiveEditorPath($config)
  123. {
  124. if (!isset($this->activeEditorPath) || $this->activeEditorPath !== $config->getData('activeEditorPath')) {
  125. $this->activeEditorPath = $config->getData('activeEditorPath')
  126. ? $config->getData('activeEditorPath')
  127. : $this->activeEditor->getWysiwygAdapterPath();
  128. $config->setData('activeEditorPath', $this->activeEditorPath);
  129. }
  130. return $this->activeEditorPath;
  131. }
  132. /**
  133. * Update config using config provider by active editor path
  134. *
  135. * @param \Magento\Framework\DataObject $config
  136. * @param array $configProviders
  137. * @return \Magento\Framework\DataObject
  138. */
  139. private function updateConfig($config, array $configProviders)
  140. {
  141. $adapterType = $this->getActiveEditorPath($config);
  142. //Extension point to update plugin settings by adapter type
  143. $providerClass = isset($configProviders[$adapterType])
  144. ? $configProviders[$adapterType]
  145. : $configProviders['default'];
  146. /** @var \Magento\Framework\Data\Wysiwyg\ConfigProviderInterface $provider */
  147. $provider = $this->configProviderFactory->create($providerClass);
  148. return $provider->getConfig($config);
  149. }
  150. }