Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Attributes
Menu Class Reference
Inheritance diagram for Menu:

Public Member Functions

 __construct (LoggerInterface $logger, $pathInMenuStructure='', Factory $menuItemFactory=null, SerializerInterface $serializer=null)
 
 add (Item $item, $parentId=null, $index=null)
 
 move ($itemId, $toItemId, $sortIndex=null)
 
 isLast (Item $item)
 
 getParentItems ($itemId)
 
 serialize ()
 
 toArray ()
 
 unserialize ($serialized)
 
 populateFromArray (array $data)
 

Protected Attributes

 $_path = ''
 
 $_logger
 

Detailed Description

Backend menu model

@api

Since
100.0.2

Definition at line 20 of file Menu.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( LoggerInterface  $logger,
  $pathInMenuStructure = '',
Factory  $menuItemFactory = null,
SerializerInterface  $serializer = null 
)

Menu constructor

Parameters
LoggerInterface$logger
string$pathInMenuStructure
Factory | null$menuItemFactory
SerializerInterface | null$serializer

Definition at line 52 of file Menu.php.

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  }
$logger

Member Function Documentation

◆ add()

add ( Item  $item,
  $parentId = null,
  $index = null 
)

Add child to menu item

Parameters
Item$item
string$parentId
int$index
Returns
void
Exceptions

Definition at line 77 of file Menu.php.

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  }
add(Item $item, $parentId=null, $index=null)
Definition: Menu.php:77
$index
Definition: list.phtml:44

◆ getParentItems()

getParentItems (   $itemId)

Get parent items by item id

Parameters
string$itemId
Returns
Item[]

Definition at line 233 of file Menu.php.

234  {
235  $parents = [];
236  $this->_findParentItems($this, $itemId, $parents);
237  return array_reverse($parents);
238  }

◆ isLast()

isLast ( Item  $item)

Check whether provided item is last in list

Parameters
Item$item
Returns
bool

Definition at line 197 of file Menu.php.

198  {
199  return $this->offsetGet(max(array_keys($this->getArrayCopy())))->getId() == $item->getId();
200  }

◆ move()

move (   $itemId,
  $toItemId,
  $sortIndex = null 
)

Move menu item

Parameters
string$itemId
string$toItemId
int$sortIndex
Returns
void
Exceptions

Definition at line 130 of file Menu.php.

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  }
add(Item $item, $parentId=null, $index=null)
Definition: Menu.php:77

◆ populateFromArray()

populateFromArray ( array  $data)

Populate the menu with data from array

Parameters
array$data
Returns
void
Since
100.2.0

Definition at line 310 of file Menu.php.

311  {
312  $items = [];
313  foreach ($data as $itemData) {
314  $item = $this->menuItemFactory->create($itemData);
315  $items[] = $item;
316  }
317  $this->exchangeArray($items);
318  }
$items

◆ serialize()

serialize ( )

Serialize menu

Returns
string

Definition at line 270 of file Menu.php.

271  {
272  return $this->serializer->serialize($this->toArray());
273  }

◆ toArray()

toArray ( )

Get menu data represented as an array

Returns
array
Since
100.2.0

Definition at line 281 of file Menu.php.

282  {
283  $data = [];
284  foreach ($this as $item) {
285  $data[] = $item->toArray();
286  }
287  return $data;
288  }

◆ unserialize()

unserialize (   $serialized)

Unserialize menu

Parameters
string$serialized
Returns
void
Since
100.2.0

Definition at line 297 of file Menu.php.

298  {
299  $data = $this->serializer->unserialize($serialized);
300  $this->populateFromArray($data);
301  }
populateFromArray(array $data)
Definition: Menu.php:310

Field Documentation

◆ $_logger

$_logger
protected

Definition at line 32 of file Menu.php.

◆ $_path

$_path = ''
protected

Definition at line 27 of file Menu.php.


The documentation for this class was generated from the following file: