12 use Psr\Log\LoggerInterface;
20 class Menu extends \ArrayObject
37 private $menuItemFactory;
54 $pathInMenuStructure =
'',
55 Factory $menuItemFactory =
null,
58 if ($pathInMenuStructure) {
59 $this->_path = $pathInMenuStructure .
'/';
62 $this->setIteratorClass(\
Magento\Backend\Model\
Menu\Iterator::class);
64 ->create(Factory::class);
79 if ($parentId !==
null) {
80 $parentItem = $this->
get($parentId);
81 if ($parentItem ===
null) {
82 throw new \InvalidArgumentException(
"Item with identifier {$parentId} does not exist");
84 $parentItem->getChildren()->add(
$item,
null,
$index);
87 if (!isset($this[
$index])) {
90 sprintf(
'Add of item with id %s was processed',
$item->getId())
104 public function get($itemId)
108 foreach ($this as
$item) {
109 if (
$item->getId() == $itemId) {
130 public function move($itemId, $toItemId, $sortIndex =
null)
132 $item = $this->
get($itemId);
133 if (
$item ===
null) {
134 throw new \InvalidArgumentException(
"Item with identifier {$itemId} does not exist");
136 $this->
remove($itemId);
137 $this->
add($item, $toItemId, $sortIndex);
146 public function remove($itemId)
150 foreach ($this as $key =>
$item) {
151 if (
$item->getId() == $itemId) {
154 $this->_logger->info(
155 sprintf(
'Remove on item with id %s was processed',
$item->getId())
174 public function reorder($itemId, $position)
178 foreach ($this as $key =>
$item) {
179 if (
$item->getId() == $itemId) {
181 $this->
add($item,
null, $position);
199 return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() ==
$item->getId();
207 public function getFirstAvailable()
211 foreach ($this as
$item) {
212 if (
$item->isAllowed() && !
$item->isDisabled()) {
213 if (
$item->hasChildren()) {
215 if (
false == (
$result ===
null)) {
236 $this->_findParentItems($this, $itemId, $parents);
237 return array_reverse($parents);
248 protected function _findParentItems($menu, $itemId, &$parents)
251 foreach ($menu as
$item) {
252 if (
$item->getId() == $itemId) {
255 if (
$item->hasChildren()) {
256 if ($this->_findParentItems(
$item->getChildren(), $itemId, $parents)) {
272 return $this->serializer->serialize($this->
toArray());
284 foreach ($this as
$item) {
299 $data = $this->serializer->unserialize($serialized);
313 foreach (
$data as $itemData) {
314 $item = $this->menuItemFactory->create($itemData);
317 $this->exchangeArray(
$items);
elseif(isset( $params[ 'redirect_parent']))