AbstractElement.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Config\Model\Config\Structure;
  7. use Magento\Config\Model\Config\StructureElementInterface;
  8. use Magento\Framework\App\Config\ScopeConfigInterface;
  9. use Magento\Store\Model\StoreManagerInterface;
  10. use Magento\Framework\App\ObjectManager;
  11. /**
  12. * @api
  13. * @since 100.0.2
  14. */
  15. abstract class AbstractElement implements StructureElementInterface
  16. {
  17. /**
  18. * Element data
  19. *
  20. * @var array
  21. */
  22. protected $_data = [];
  23. /**
  24. * Current configuration scope
  25. *
  26. * @var string
  27. */
  28. protected $_scope;
  29. /**
  30. * Store manager
  31. *
  32. * @var \Magento\Store\Model\StoreManagerInterface
  33. */
  34. protected $_storeManager;
  35. /**
  36. * @var \Magento\Framework\Module\Manager
  37. */
  38. protected $moduleManager;
  39. /**
  40. * @var ElementVisibilityInterface
  41. */
  42. private $elementVisibility;
  43. /**
  44. * @param \Magento\Store\Model\StoreManagerInterface $storeManager
  45. * @param \Magento\Framework\Module\Manager $moduleManager
  46. */
  47. public function __construct(StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager)
  48. {
  49. $this->_storeManager = $storeManager;
  50. $this->moduleManager = $moduleManager;
  51. }
  52. /**
  53. * Translate element attribute
  54. *
  55. * @param string $code
  56. * @return \Magento\Framework\Phrase|string
  57. */
  58. protected function _getTranslatedAttribute($code)
  59. {
  60. if (false == array_key_exists($code, $this->_data)) {
  61. return '';
  62. }
  63. return __($this->_data[$code]);
  64. }
  65. /**
  66. * Set element data
  67. *
  68. * @param array $data
  69. * @param string $scope
  70. * @return void
  71. */
  72. public function setData(array $data, $scope)
  73. {
  74. $this->_data = $data;
  75. $this->_scope = $scope;
  76. }
  77. /**
  78. * Retrieve flyweight data
  79. *
  80. * @return array
  81. */
  82. public function getData()
  83. {
  84. return $this->_data;
  85. }
  86. /**
  87. * Retrieve element id
  88. *
  89. * @return string
  90. */
  91. public function getId()
  92. {
  93. return isset($this->_data['id']) ? $this->_data['id'] : '';
  94. }
  95. /**
  96. * Retrieve element label
  97. *
  98. * @return string
  99. */
  100. public function getLabel()
  101. {
  102. return $this->_getTranslatedAttribute('label');
  103. }
  104. /**
  105. * Retrieve element label
  106. *
  107. * @return string
  108. */
  109. public function getComment()
  110. {
  111. return $this->_getTranslatedAttribute('comment');
  112. }
  113. /**
  114. * Retrieve frontend model class name
  115. *
  116. * @return string
  117. */
  118. public function getFrontendModel()
  119. {
  120. return isset($this->_data['frontend_model']) ? $this->_data['frontend_model'] : '';
  121. }
  122. /**
  123. * Retrieve arbitrary element attribute
  124. *
  125. * @param string $key
  126. * @return mixed
  127. */
  128. public function getAttribute($key)
  129. {
  130. return array_key_exists($key, $this->_data) ? $this->_data[$key] : null;
  131. }
  132. /**
  133. * Check whether element should be displayed
  134. *
  135. * @return bool
  136. */
  137. public function isVisible()
  138. {
  139. if ($this->getElementVisibility()->isHidden($this->getPath())) {
  140. return false;
  141. }
  142. if (isset($this->_data['if_module_enabled']) &&
  143. !$this->moduleManager->isOutputEnabled($this->_data['if_module_enabled'])) {
  144. return false;
  145. }
  146. $showInScope = [
  147. \Magento\Store\Model\ScopeInterface::SCOPE_STORE => $this->_hasVisibilityValue('showInStore'),
  148. \Magento\Store\Model\ScopeInterface::SCOPE_WEBSITE => $this->_hasVisibilityValue('showInWebsite'),
  149. ScopeConfigInterface::SCOPE_TYPE_DEFAULT => $this->_hasVisibilityValue('showInDefault'),
  150. ];
  151. if ($this->_storeManager->isSingleStoreMode()) {
  152. $result = !$this->_hasVisibilityValue('hide_in_single_store_mode') && array_sum($showInScope);
  153. return $result;
  154. }
  155. return !empty($showInScope[$this->_scope]);
  156. }
  157. /**
  158. * Retrieve value of visibility flag
  159. *
  160. * @param string $key
  161. * @return bool
  162. */
  163. protected function _hasVisibilityValue($key)
  164. {
  165. return isset($this->_data[$key]) && $this->_data[$key];
  166. }
  167. /**
  168. * Retrieve css class of a tab
  169. *
  170. * @return string
  171. */
  172. public function getClass()
  173. {
  174. return isset($this->_data['class']) ? $this->_data['class'] : '';
  175. }
  176. /**
  177. * Retrieve config path for given id
  178. *
  179. * @param string $fieldId
  180. * @param string $fieldPrefix
  181. * @return string
  182. */
  183. protected function _getPath($fieldId, $fieldPrefix = '')
  184. {
  185. $path = isset($this->_data['path']) ? $this->_data['path'] : '';
  186. return $path . '/' . $fieldPrefix . $fieldId;
  187. }
  188. /**
  189. * Retrieve element config path
  190. *
  191. * @param string $fieldPrefix
  192. * @return string
  193. */
  194. public function getPath($fieldPrefix = '')
  195. {
  196. return $this->_getPath($this->getId(), $fieldPrefix);
  197. }
  198. /**
  199. * Get instance of ElementVisibilityInterface.
  200. *
  201. * @return ElementVisibilityInterface
  202. * @deprecated 101.0.0 Added to not break backward compatibility of the constructor signature
  203. * by injecting the new dependency directly.
  204. * The method can be removed in a future major release, when constructor signature can be changed.
  205. * @since 101.0.0
  206. */
  207. public function getElementVisibility()
  208. {
  209. if (null === $this->elementVisibility) {
  210. $this->elementVisibility = ObjectManager::getInstance()->get(ElementVisibilityInterface::class);
  211. }
  212. return $this->elementVisibility;
  213. }
  214. }