Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Builder.php
Go to the documentation of this file.
1 <?php
7 
14 class Builder
15 {
19  protected $_commands = [];
20 
24  protected $_itemFactory;
25 
29  public function __construct(\Magento\Backend\Model\Menu\Item\Factory $menuItemFactory)
30  {
31  $this->_itemFactory = $menuItemFactory;
32  }
33 
40  public function processCommand(\Magento\Backend\Model\Menu\Builder\AbstractCommand $command)
41  {
42  if (!isset($this->_commands[$command->getId()])) {
43  $this->_commands[$command->getId()] = $command;
44  } else {
45  $this->_commands[$command->getId()]->chain($command);
46  }
47  return $this;
48  }
49 
57  public function getResult(\Magento\Backend\Model\Menu $menu)
58  {
60  $params = [];
61  $items = [];
62 
63  // Create menu items
64  foreach ($this->_commands as $id => $command) {
65  $params[$id] = $command->execute();
66  $item = $this->_itemFactory->create($params[$id]);
67  $items[$id] = $item;
68  }
69 
70  // Build menu tree based on "parent" param
71  foreach ($items as $id => $item) {
72  $sortOrder = $this->_getParam($params[$id], 'sortOrder');
73  $parentId = $this->_getParam($params[$id], 'parent');
74  $isRemoved = isset($params[$id]['removed']);
75 
76  if ($isRemoved) {
77  continue;
78  }
79  if (!$parentId) {
80  $menu->add($item, null, $sortOrder);
81  } else {
82  if (!isset($items[$parentId])) {
83  throw new \OutOfRangeException(sprintf('Specified invalid parent id (%s)', $parentId));
84  }
85  if (isset($params[$parentId]['removed'])) {
86  continue;
87  }
88  $items[$parentId]->getChildren()->add($item, null, $sortOrder);
89  }
90  }
91 
92  return $menu;
93  }
94 
103  protected function _getParam($params, $paramName, $defaultValue = null)
104  {
105  return $params[$paramName] ?? $defaultValue;
106  }
107 }
$id
Definition: fieldset.phtml:14
_getParam($params, $paramName, $defaultValue=null)
Definition: Builder.php:103
__construct(\Magento\Backend\Model\Menu\Item\Factory $menuItemFactory)
Definition: Builder.php:29
processCommand(\Magento\Backend\Model\Menu\Builder\AbstractCommand $command)
Definition: Builder.php:40
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$items