Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Menu.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Backend\Model;
7 
12 use Psr\Log\LoggerInterface;
13 
20 class Menu extends \ArrayObject
21 {
27  protected $_path = '';
28 
32  protected $_logger;
33 
37  private $menuItemFactory;
38 
42  private $serializer;
43 
52  public function __construct(
53  LoggerInterface $logger,
54  $pathInMenuStructure = '',
55  Factory $menuItemFactory = null,
56  SerializerInterface $serializer = null
57  ) {
58  if ($pathInMenuStructure) {
59  $this->_path = $pathInMenuStructure . '/';
60  }
61  $this->_logger = $logger;
62  $this->setIteratorClass(\Magento\Backend\Model\Menu\Iterator::class);
63  $this->menuItemFactory = $menuItemFactory ?: ObjectManager::getInstance()
64  ->create(Factory::class);
65  $this->serializer = $serializer ?: ObjectManager::getInstance()->create(SerializerInterface::class);
66  }
67 
77  public function add(Item $item, $parentId = null, $index = null)
78  {
79  if ($parentId !== null) {
80  $parentItem = $this->get($parentId);
81  if ($parentItem === null) {
82  throw new \InvalidArgumentException("Item with identifier {$parentId} does not exist");
83  }
84  $parentItem->getChildren()->add($item, null, $index);
85  } else {
86  $index = (int) $index;
87  if (!isset($this[$index])) {
88  $this->offsetSet($index, $item);
89  $this->_logger->info(
90  sprintf('Add of item with id %s was processed', $item->getId())
91  );
92  } else {
93  $this->add($item, $parentId, $index + 1);
94  }
95  }
96  }
97 
104  public function get($itemId)
105  {
106  $result = null;
108  foreach ($this as $item) {
109  if ($item->getId() == $itemId) {
110  $result = $item;
111  break;
112  }
113 
114  if ($item->hasChildren() && ($result = $item->getChildren()->get($itemId))) {
115  break;
116  }
117  }
118  return $result;
119  }
120 
130  public function move($itemId, $toItemId, $sortIndex = null)
131  {
132  $item = $this->get($itemId);
133  if ($item === null) {
134  throw new \InvalidArgumentException("Item with identifier {$itemId} does not exist");
135  }
136  $this->remove($itemId);
137  $this->add($item, $toItemId, $sortIndex);
138  }
139 
146  public function remove($itemId)
147  {
148  $result = false;
150  foreach ($this as $key => $item) {
151  if ($item->getId() == $itemId) {
152  unset($this[$key]);
153  $result = true;
154  $this->_logger->info(
155  sprintf('Remove on item with id %s was processed', $item->getId())
156  );
157  break;
158  }
159 
160  if ($item->hasChildren() && ($result = $item->getChildren()->remove($itemId))) {
161  break;
162  }
163  }
164  return $result;
165  }
166 
174  public function reorder($itemId, $position)
175  {
176  $result = false;
178  foreach ($this as $key => $item) {
179  if ($item->getId() == $itemId) {
180  unset($this[$key]);
181  $this->add($item, null, $position);
182  $result = true;
183  break;
184  } elseif ($item->hasChildren() && $result = $item->getChildren()->reorder($itemId, $position)) {
185  break;
186  }
187  }
188  return $result;
189  }
190 
197  public function isLast(Item $item)
198  {
199  return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() == $item->getId();
200  }
201 
207  public function getFirstAvailable()
208  {
209  $result = null;
211  foreach ($this as $item) {
212  if ($item->isAllowed() && !$item->isDisabled()) {
213  if ($item->hasChildren()) {
214  $result = $item->getChildren()->getFirstAvailable();
215  if (false == ($result === null)) {
216  break;
217  }
218  } else {
219  $result = $item;
220  break;
221  }
222  }
223  }
224  return $result;
225  }
226 
233  public function getParentItems($itemId)
234  {
235  $parents = [];
236  $this->_findParentItems($this, $itemId, $parents);
237  return array_reverse($parents);
238  }
239 
248  protected function _findParentItems($menu, $itemId, &$parents)
249  {
251  foreach ($menu as $item) {
252  if ($item->getId() == $itemId) {
253  return true;
254  }
255  if ($item->hasChildren()) {
256  if ($this->_findParentItems($item->getChildren(), $itemId, $parents)) {
257  $parents[] = $item;
258  return true;
259  }
260  }
261  }
262  return false;
263  }
264 
270  public function serialize()
271  {
272  return $this->serializer->serialize($this->toArray());
273  }
274 
281  public function toArray()
282  {
283  $data = [];
284  foreach ($this as $item) {
285  $data[] = $item->toArray();
286  }
287  return $data;
288  }
289 
297  public function unserialize($serialized)
298  {
299  $data = $this->serializer->unserialize($serialized);
300  $this->populateFromArray($data);
301  }
302 
310  public function populateFromArray(array $data)
311  {
312  $items = [];
313  foreach ($data as $itemData) {
314  $item = $this->menuItemFactory->create($itemData);
315  $items[] = $item;
316  }
317  $this->exchangeArray($items);
318  }
319 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
populateFromArray(array $data)
Definition: Menu.php:310
move($itemId, $toItemId, $sortIndex=null)
Definition: Menu.php:130
$logger
__construct(LoggerInterface $logger, $pathInMenuStructure='', Factory $menuItemFactory=null, SerializerInterface $serializer=null)
Definition: Menu.php:52
add(Item $item, $parentId=null, $index=null)
Definition: Menu.php:77
isLast(Item $item)
Definition: Menu.php:197
unserialize($serialized)
Definition: Menu.php:297
$index
Definition: list.phtml:44
$items