Config.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. use Magento\Framework\Filesystem;
  8. use Magento\Framework\App\Filesystem\DirectoryList;
  9. use Magento\Ui\Component\Wysiwyg\ConfigInterface;
  10. use Magento\Framework\App\ObjectManager;
  11. /**
  12. * Wysiwyg Config for Editor HTML Element
  13. *
  14. * @api
  15. * @since 100.0.2
  16. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  17. */
  18. class Config extends \Magento\Framework\DataObject implements ConfigInterface
  19. {
  20. /**
  21. * Wysiwyg status enabled
  22. */
  23. const WYSIWYG_ENABLED = 'enabled';
  24. /**
  25. * Wysiwyg status configuration path
  26. */
  27. const WYSIWYG_STATUS_CONFIG_PATH = 'cms/wysiwyg/enabled';
  28. /**
  29. *
  30. */
  31. const WYSIWYG_SKIN_IMAGE_PLACEHOLDER_ID = 'Magento_Cms::images/wysiwyg_skin_image.png';
  32. /**
  33. * Wysiwyg status hidden
  34. */
  35. const WYSIWYG_HIDDEN = 'hidden';
  36. /**
  37. * Wysiwyg status disabled
  38. */
  39. const WYSIWYG_DISABLED = 'disabled';
  40. /**
  41. * Wysiwyg image directory
  42. */
  43. const IMAGE_DIRECTORY = 'wysiwyg';
  44. /**
  45. * @var \Magento\Framework\AuthorizationInterface
  46. */
  47. protected $_authorization;
  48. /**
  49. * @var \Magento\Framework\View\Asset\Repository
  50. */
  51. protected $_assetRepo;
  52. /**
  53. * @var \Magento\Variable\Model\Variable\Config
  54. * @deprecated 103.0.0
  55. * @see \Magento\Cms\Model\ConfigProvider::processVariableConfig
  56. */
  57. protected $_variableConfig;
  58. /**
  59. * @var \Magento\Widget\Model\Widget\Config
  60. * @deprecated 103.0.0
  61. * @see \Magento\Cms\Model\ConfigProvider::processWidgetConfig
  62. */
  63. protected $_widgetConfig;
  64. /**
  65. * Core event manager proxy
  66. *
  67. * @var \Magento\Framework\Event\ManagerInterface
  68. */
  69. protected $_eventManager;
  70. /**
  71. * Core store config
  72. *
  73. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  74. */
  75. protected $_scopeConfig;
  76. /**
  77. * @var array
  78. */
  79. protected $_windowSize;
  80. /**
  81. * @var \Magento\Backend\Model\UrlInterface
  82. */
  83. protected $_backendUrl;
  84. /**
  85. * @var \Magento\Store\Model\StoreManagerInterface
  86. */
  87. protected $_storeManager;
  88. /**
  89. * @var Filesystem
  90. * @since 101.0.0
  91. */
  92. protected $filesystem;
  93. /**
  94. * @var \Magento\Cms\Model\Wysiwyg\CompositeConfigProvider
  95. */
  96. private $configProvider;
  97. /**
  98. * Config constructor.
  99. * @param \Magento\Backend\Model\UrlInterface $backendUrl
  100. * @param \Magento\Framework\Event\ManagerInterface $eventManager
  101. * @param \Magento\Framework\AuthorizationInterface $authorization
  102. * @param \Magento\Framework\View\Asset\Repository $assetRepo
  103. * @param \Magento\Variable\Model\Variable\Config $variableConfig
  104. * @param \Magento\Widget\Model\Widget\Config $widgetConfig
  105. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  106. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  107. * @param Filesystem $filesystem
  108. * @param array $windowSize
  109. * @param array $data
  110. * @param CompositeConfigProvider|null $configProvider
  111. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  112. */
  113. public function __construct(
  114. \Magento\Backend\Model\UrlInterface $backendUrl,
  115. \Magento\Framework\Event\ManagerInterface $eventManager,
  116. \Magento\Framework\AuthorizationInterface $authorization,
  117. \Magento\Framework\View\Asset\Repository $assetRepo,
  118. \Magento\Variable\Model\Variable\Config $variableConfig,
  119. \Magento\Widget\Model\Widget\Config $widgetConfig,
  120. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  121. \Magento\Store\Model\StoreManagerInterface $storeManager,
  122. Filesystem $filesystem,
  123. array $windowSize = [],
  124. array $data = [],
  125. \Magento\Cms\Model\Wysiwyg\CompositeConfigProvider $configProvider = null
  126. ) {
  127. $this->_backendUrl = $backendUrl;
  128. $this->_eventManager = $eventManager;
  129. $this->_scopeConfig = $scopeConfig;
  130. $this->_authorization = $authorization;
  131. $this->_assetRepo = $assetRepo;
  132. $this->_variableConfig = $variableConfig;
  133. $this->_widgetConfig = $widgetConfig;
  134. $this->_windowSize = $windowSize;
  135. $this->_storeManager = $storeManager;
  136. $this->filesystem = $filesystem;
  137. $this->configProvider = $configProvider ?: ObjectManager::getInstance()
  138. ->get(\Magento\Cms\Model\Wysiwyg\CompositeConfigProvider ::class);
  139. parent::__construct($data);
  140. }
  141. /**
  142. * Return Wysiwyg config as \Magento\Framework\DataObject
  143. *
  144. * Config options description:
  145. *
  146. * enabled: Enabled Visual Editor or not
  147. * hidden: Show Visual Editor on page load or not
  148. * use_container: Wrap Editor contents into div or not
  149. * no_display: Hide Editor container or not (related to use_container)
  150. * translator: Helper to translate phrases in lib
  151. * files_browser_*: Files Browser (media, images) settings
  152. * encode_directives: Encode template directives with JS or not
  153. *
  154. * @param array|\Magento\Framework\DataObject $data Object constructor params to override default config values
  155. * @return \Magento\Framework\DataObject
  156. */
  157. public function getConfig($data = [])
  158. {
  159. $config = new \Magento\Framework\DataObject();
  160. $config->setData(
  161. [
  162. 'enabled' => $this->isEnabled(),
  163. 'hidden' => $this->isHidden(),
  164. 'baseStaticUrl' => $this->_assetRepo->getStaticViewFileContext()->getBaseUrl(),
  165. 'baseStaticDefaultUrl' => str_replace('index.php/', '', $this->_backendUrl->getBaseUrl())
  166. . $this->filesystem->getUri(DirectoryList::STATIC_VIEW) . '/',
  167. 'directives_url' => $this->_backendUrl->getUrl('cms/wysiwyg/directive'),
  168. 'use_container' => false,
  169. 'add_variables' => true,
  170. 'add_widgets' => true,
  171. 'no_display' => false,
  172. 'add_directives' => true,
  173. 'width' => '100%',
  174. 'height' => '500px',
  175. 'plugins' => [],
  176. ]
  177. );
  178. $config->setData('directives_url_quoted', preg_quote($config->getData('directives_url')));
  179. if (is_array($data)) {
  180. $config->addData($data);
  181. }
  182. if ($this->_authorization->isAllowed('Magento_Cms::media_gallery')) {
  183. $this->configProvider->processGalleryConfig($config);
  184. $config->addData(
  185. [
  186. 'files_browser_window_width' => $this->_windowSize['width'],
  187. 'files_browser_window_height' => $this->_windowSize['height'],
  188. ]
  189. );
  190. }
  191. if ($config->getData('add_widgets')) {
  192. $this->configProvider->processWidgetConfig($config);
  193. }
  194. if ($config->getData('add_variables')) {
  195. $this->configProvider->processVariableConfig($config);
  196. }
  197. return $this->configProvider->processWysiwygConfig($config);
  198. }
  199. /**
  200. * Return path for skin images placeholder
  201. *
  202. * @return string
  203. */
  204. public function getSkinImagePlaceholderPath()
  205. {
  206. $staticPath = $this->_storeManager->getStore()->getBaseStaticDir();
  207. $placeholderPath = $this->_assetRepo->createAsset(self::WYSIWYG_SKIN_IMAGE_PLACEHOLDER_ID)->getPath();
  208. return $staticPath . '/' . $placeholderPath;
  209. }
  210. /**
  211. * Check whether Wysiwyg is enabled or not
  212. *
  213. * @return bool
  214. */
  215. public function isEnabled()
  216. {
  217. $wysiwygState = $this->_scopeConfig->getValue(
  218. self::WYSIWYG_STATUS_CONFIG_PATH,
  219. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  220. $this->getStoreId()
  221. );
  222. return in_array($wysiwygState, [self::WYSIWYG_ENABLED, self::WYSIWYG_HIDDEN]);
  223. }
  224. /**
  225. * Check whether Wysiwyg is loaded on demand or not
  226. *
  227. * @return bool
  228. */
  229. public function isHidden()
  230. {
  231. $status = $this->_scopeConfig->getValue(
  232. self::WYSIWYG_STATUS_CONFIG_PATH,
  233. \Magento\Store\Model\ScopeInterface::SCOPE_STORE
  234. );
  235. return $status == self::WYSIWYG_HIDDEN;
  236. }
  237. }