Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Helper.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class Helper
12 {
24  protected $counter = 0;
25 
29  protected $logger;
30 
34  protected $state;
35 
40  public function __construct(
41  \Psr\Log\LoggerInterface $logger,
43  ) {
44  $this->logger = $logger;
45  $this->state = $state;
46  }
47 
54  protected function _generateAnonymousName($class)
55  {
56  $position = strpos($class, '\\Block\\');
57  $key = $position !== false ? substr($class, $position + 7) : $class;
58  $key = strtolower(trim($key, '_'));
59  return $key . $this->counter++;
60  }
61 
71  public function scheduleStructure(
73  Layout\Element $currentNode,
74  Layout\Element $parentNode
75  ) {
76  // if it hasn't a name it must be generated
77  if (!(string)$currentNode->getAttribute('name')) {
78  $name = $this->_generateAnonymousName($parentNode->getElementName() . '_schedule_block');
79  $currentNode->setAttribute('name', $name);
80  }
81  $path = $name = (string)$currentNode->getAttribute('name');
82 
83  // Prepare scheduled element with default parameters [type, alias, parentName, siblingName, isAfter]
84  $row = [
85  self::SCHEDULED_STRUCTURE_INDEX_TYPE => $currentNode->getName(),
86  self::SCHEDULED_STRUCTURE_INDEX_ALIAS => '',
87  self::SCHEDULED_STRUCTURE_INDEX_PARENT_NAME => '',
88  self::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME => null,
89  self::SCHEDULED_STRUCTURE_INDEX_IS_AFTER => true,
90  ];
91 
92  $parentName = $parentNode->getElementName();
93  //if this element has a parent element, there must be reset [alias, parentName, siblingName, isAfter]
94  if ($parentName) {
95  $row[self::SCHEDULED_STRUCTURE_INDEX_ALIAS] = (string)$currentNode->getAttribute('as');
97 
98  list($row[self::SCHEDULED_STRUCTURE_INDEX_SIBLING_NAME],
99  $row[self::SCHEDULED_STRUCTURE_INDEX_IS_AFTER]) = $this->_beforeAfterToSibling($currentNode);
100 
101  // materialized path for referencing nodes in the plain array of _scheduledStructure
102  if ($scheduledStructure->hasPath($parentName)) {
103  $path = $scheduledStructure->getPath($parentName) . '/' . $path;
104  }
105  }
106 
107  $this->_overrideElementWorkaround($scheduledStructure, $name, $path);
108  $scheduledStructure->setPathElement($name, $path);
109  $scheduledStructure->setStructureElement($name, $row);
110  return $name;
111  }
112 
125  {
126  if ($scheduledStructure->hasStructureElement($name)) {
127  $scheduledStructure->setStructureElementData($name, []);
128  foreach ($scheduledStructure->getPaths() as $potentialChild => $childPath) {
129  if (0 === strpos($childPath, "{$path}/")) {
130  $scheduledStructure->unsetPathElement($potentialChild);
131  $scheduledStructure->unsetStructureElement($potentialChild);
132  }
133  }
134  }
135  }
136 
143  protected function _beforeAfterToSibling($node)
144  {
145  $result = [null, true];
146  if (isset($node['after'])) {
147  $result[0] = (string)$node['after'];
148  } elseif (isset($node['before'])) {
149  $result[0] = (string)$node['before'];
150  $result[1] = false;
151  }
152  return $result;
153  }
154 
169  public function scheduleElement(
171  Layout\Data\Structure $structure,
172  $key
173  ) {
174  $row = $scheduledStructure->getStructureElement($key);
175  $data = $scheduledStructure->getStructureElementData($key);
176  // if we have reference container to not existed element
177  if (!isset($row[self::SCHEDULED_STRUCTURE_INDEX_TYPE])) {
178  $this->logger->critical("Broken reference: missing declaration of the element '{$key}'.");
179  $scheduledStructure->unsetPathElement($key);
180  $scheduledStructure->unsetStructureElement($key);
181  return;
182  }
183  list($type, $alias, $parentName, $siblingName, $isAfter) = $row;
184  $name = $this->_createStructuralElement($structure, $key, $type, $parentName . $alias);
185  if ($parentName) {
186  // recursively populate parent first
187  if ($scheduledStructure->hasStructureElement($parentName)) {
188  $this->scheduleElement($scheduledStructure, $structure, $parentName);
189  }
190  if ($structure->hasElement($parentName)) {
191  try {
192  $structure->setAsChild($name, $parentName, $alias);
193  } catch (\Exception $e) {
194  $this->logger->critical($e);
195  }
196  } else {
197  $scheduledStructure->setElementToBrokenParentList($key);
198 
199  if ($this->state->getMode() === State::MODE_DEVELOPER) {
200  $this->logger->info(
201  "Broken reference: the '{$name}' element cannot be added as child to '{$parentName}', " .
202  'because the latter doesn\'t exist'
203  );
204  }
205  }
206  }
207 
208  // Move from scheduledStructure to scheduledElement
209  $scheduledStructure->unsetStructureElement($key);
210  $scheduledStructure->setElement($name, [$type, $data]);
211 
216  if ($siblingName) {
217  $scheduledStructure->setElementToSortList($parentName, $name, $siblingName, $isAfter);
218  }
219  }
220 
233  {
234  if (empty($name)) {
236  }
237  $structure->createElement($name, ['type' => $type]);
238  return $name;
239  }
240 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
__construct(\Psr\Log\LoggerInterface $logger, State $state)
Definition: Helper.php:40
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
scheduleStructure(Layout\ScheduledStructure $scheduledStructure, Layout\Element $currentNode, Layout\Element $parentNode)
Definition: Helper.php:71
_createStructuralElement(Layout\Data\Structure $structure, $name, $type, $class)
Definition: Helper.php:232
scheduleElement(Layout\ScheduledStructure $scheduledStructure, Layout\Data\Structure $structure, $key)
Definition: Helper.php:169
if(!trim($html)) $alias
Definition: details.phtml:20
_overrideElementWorkaround(Layout\ScheduledStructure $scheduledStructure, $name, $path)
Definition: Helper.php:124
if(!isset($_GET['name'])) $name
Definition: log.php:14