Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Structure.php
Go to the documentation of this file.
1 <?php
7 
9 
47 class Structure implements \Magento\Config\Model\Config\Structure\SearchInterface
48 {
52  const TYPE_KEY = '_elementType';
53 
59  protected $_data;
60 
66  protected $_tabIterator;
67 
73  protected $_flyweightFactory;
74 
80  protected $_scopeDefiner;
81 
87  protected $_elements;
88 
95  protected $sectionList;
96 
113  private $mappedPaths;
114 
121  public function __construct(
122  \Magento\Config\Model\Config\Structure\Data $structureData,
123  \Magento\Config\Model\Config\Structure\Element\Iterator\Tab $tabIterator,
124  \Magento\Config\Model\Config\Structure\Element\FlyweightFactory $flyweightFactory,
125  ScopeDefiner $scopeDefiner
126  ) {
127  $this->_data = $structureData->get();
128  $this->_tabIterator = $tabIterator;
129  $this->_flyweightFactory = $flyweightFactory;
130  $this->_scopeDefiner = $scopeDefiner;
131  }
132 
138  public function getTabs()
139  {
140  if (isset($this->_data['sections'])) {
141  foreach ($this->_data['sections'] as $sectionId => $section) {
142  if (isset($section['tab']) && $section['tab']) {
143  $this->_data['tabs'][$section['tab']]['children'][$sectionId] = $section;
144  }
145  }
146  $this->_tabIterator->setElements($this->_data['tabs'], $this->_scopeDefiner->getScope());
147  }
148  return $this->_tabIterator;
149  }
150 
158  public function getSectionList()
159  {
160  if (empty($this->sectionList)) {
161  foreach ($this->_data['sections'] as $sectionId => $section) {
162  if (array_key_exists('children', $section) && is_array($section['children'])) {
163  foreach ($section['children'] as $childId => $child) {
164  $this->sectionList[$sectionId . '_' . $childId] = true;
165  }
166  }
167  }
168  }
169  return $this->sectionList;
170  }
171 
178  public function getElement($path)
179  {
180  return $this->getElementByPathParts(explode('/', $path));
181  }
182 
190  public function getElementByConfigPath($path)
191  {
192  $allPaths = $this->getFieldPaths();
193 
194  if (isset($allPaths[$path])) {
195  $path = array_shift($allPaths[$path]);
196  }
197 
198  return $this->getElementByPathParts(explode('/', $path));
199  }
200 
207  public function getFirstSection()
208  {
209  $tabs = $this->getTabs();
210  $tabs->rewind();
212  $tab = $tabs->current();
213  $tab->getChildren()->rewind();
214  if (!$tab->getChildren()->current()->isVisible()) {
215  throw new LocalizedException(__('Visible section not found.'));
216  }
217 
218  return $tab->getChildren()->current();
219  }
220 
227  public function getElementByPathParts(array $pathParts)
228  {
229  $path = implode('_', $pathParts);
230  if (isset($this->_elements[$path])) {
231  return $this->_elements[$path];
232  }
233  $children = [];
234  if ($this->_data) {
235  $children = $this->_data['sections'];
236  }
237  $child = [];
238  foreach ($pathParts as $pathPart) {
239  if ($children && (array_key_exists($pathPart, $children))) {
240  $child = $children[$pathPart];
241  $children = array_key_exists('children', $child) ? $child['children'] : [];
242  } else {
243  $child = $this->_createEmptyElement($pathParts);
244  break;
245  }
246  }
247  $this->_elements[$path] = $this->_flyweightFactory->create($child['_elementType']);
248  $this->_elements[$path]->setData($child, $this->_scopeDefiner->getScope());
249  return $this->_elements[$path];
250  }
251 
258  protected function _createEmptyElement(array $pathParts)
259  {
260  switch (count($pathParts)) {
261  case 1:
262  $elementType = 'section';
263  break;
264  case 2:
265  $elementType = 'group';
266  break;
267  default:
268  $elementType = 'field';
269  }
270  $elementId = array_pop($pathParts);
271  return ['id' => $elementId, 'path' => implode('/', $pathParts), '_elementType' => $elementType];
272  }
273 
281  public function getFieldPathsByAttribute($attributeName, $attributeValue)
282  {
283  $result = [];
284  if (empty($this->_data['sections'])) {
285  return $result;
286  }
287 
288  foreach ($this->_data['sections'] as $section) {
289  if (!isset($section['children'])) {
290  continue;
291  }
292  foreach ($section['children'] as $group) {
293  if (isset($group['children'])) {
294  $path = $section['id'] . '/' . $group['id'];
295  $result = array_merge(
296  $result,
298  $group['children'],
299  $path,
300  $attributeName,
301  $attributeValue
302  )
303  );
304  }
305  }
306  }
307  return $result;
308  }
309 
319  protected function _getGroupFieldPathsByAttribute(array $fields, $parentPath, $attributeName, $attributeValue)
320  {
321  $result = [];
322  foreach ($fields as $field) {
323  if (isset($field['children'])) {
325  $field['children'],
326  $parentPath . '/' . $field['id'],
327  $attributeName,
328  $attributeValue
329  );
330  } elseif (isset($field[$attributeName]) && $field[$attributeName] == $attributeValue) {
331  $result[] = $parentPath . '/' . $field['id'];
332  }
333  }
334  return $result;
335  }
336 
374  public function getFieldPaths()
375  {
376  $sections = !empty($this->_data['sections']) ? $this->_data['sections'] : [];
377 
378  if (!$this->mappedPaths) {
379  $this->mappedPaths = $this->getFieldsRecursively($sections);
380  }
381 
382  return $this->mappedPaths;
383  }
384 
391  private function getFieldsRecursively(array $elements = [])
392  {
393  $result = [];
394 
395  foreach ($elements as $element) {
396  if (isset($element['children'])) {
397  $result = array_replace_recursive(
398  $result,
399  $this->getFieldsRecursively($element['children'])
400  );
401  } else {
402  if ($element['_elementType'] === 'field' && isset($element['label'])) {
403  $structurePath = (isset($element['path']) ? $element['path'] . '/' : '') . $element['id'];
404  $configPath = isset($element['config_path']) ? $element['config_path'] : $structurePath;
405 
406  if (!isset($result[$configPath])) {
407  $result[$configPath] = [];
408  }
409 
410  $result[$configPath][] = $structurePath;
411  }
412  }
413  }
414 
415  return $result;
416  }
417 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
getElementByPathParts(array $pathParts)
Definition: Structure.php:227
$group
Definition: sections.phtml:16
$fields
Definition: details.phtml:14
__()
Definition: __.php:13
_getGroupFieldPathsByAttribute(array $fields, $parentPath, $attributeName, $attributeValue)
Definition: Structure.php:319
getFieldPathsByAttribute($attributeName, $attributeValue)
Definition: Structure.php:281
$children
Definition: actions.phtml:11
__construct(\Magento\Config\Model\Config\Structure\Data $structureData, \Magento\Config\Model\Config\Structure\Element\Iterator\Tab $tabIterator, \Magento\Config\Model\Config\Structure\Element\FlyweightFactory $flyweightFactory, ScopeDefiner $scopeDefiner)
Definition: Structure.php:121
$element
Definition: element.phtml:12