Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractElement.php
Go to the documentation of this file.
1 <?php
7 
12 
17 abstract class AbstractElement implements StructureElementInterface
18 {
24  protected $_data = [];
25 
31  protected $_scope;
32 
38  protected $_storeManager;
39 
43  protected $moduleManager;
44 
48  private $elementVisibility;
49 
54  public function __construct(StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager)
55  {
56  $this->_storeManager = $storeManager;
57  $this->moduleManager = $moduleManager;
58  }
59 
66  protected function _getTranslatedAttribute($code)
67  {
68  if (false == array_key_exists($code, $this->_data)) {
69  return '';
70  }
71  return __($this->_data[$code]);
72  }
73 
81  public function setData(array $data, $scope)
82  {
83  $this->_data = $data;
84  $this->_scope = $scope;
85  }
86 
92  public function getData()
93  {
94  return $this->_data;
95  }
96 
102  public function getId()
103  {
104  return isset($this->_data['id']) ? $this->_data['id'] : '';
105  }
106 
112  public function getLabel()
113  {
114  return $this->_getTranslatedAttribute('label');
115  }
116 
122  public function getComment()
123  {
124  return $this->_getTranslatedAttribute('comment');
125  }
126 
132  public function getFrontendModel()
133  {
134  return isset($this->_data['frontend_model']) ? $this->_data['frontend_model'] : '';
135  }
136 
143  public function getAttribute($key)
144  {
145  return array_key_exists($key, $this->_data) ? $this->_data[$key] : null;
146  }
147 
153  public function isVisible()
154  {
155  if ($this->getElementVisibility()->isHidden($this->getPath())) {
156  return false;
157  }
158 
159  if (isset($this->_data['if_module_enabled']) &&
160  !$this->moduleManager->isOutputEnabled($this->_data['if_module_enabled'])) {
161  return false;
162  }
163  $showInScope = [
167  ];
168 
169  if ($this->_storeManager->isSingleStoreMode()) {
170  $result = !$this->_hasVisibilityValue('hide_in_single_store_mode') && array_sum($showInScope);
171  return $result;
172  }
173 
174  return !empty($showInScope[$this->_scope]);
175  }
176 
183  protected function _hasVisibilityValue($key)
184  {
185  return isset($this->_data[$key]) && $this->_data[$key];
186  }
187 
193  public function getClass()
194  {
195  return isset($this->_data['class']) ? $this->_data['class'] : '';
196  }
197 
205  protected function _getPath($fieldId, $fieldPrefix = '')
206  {
207  $path = isset($this->_data['path']) ? $this->_data['path'] : '';
208  return $path . '/' . $fieldPrefix . $fieldId;
209  }
210 
217  public function getPath($fieldPrefix = '')
218  {
219  return $this->_getPath($this->getId(), $fieldPrefix);
220  }
221 
231  public function getElementVisibility()
232  {
233  if (null === $this->elementVisibility) {
234  $this->elementVisibility = ObjectManager::getInstance()->get(ElementVisibilityInterface::class);
235  }
236 
237  return $this->elementVisibility;
238  }
239 }
$storeManager
__()
Definition: __.php:13
$fieldId
Definition: element.phtml:16
__construct(StoreManagerInterface $storeManager, \Magento\Framework\Module\Manager $moduleManager)
$code
Definition: info.phtml:12