Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Page.php
Go to the documentation of this file.
1 <?php
9 
15 {
21  protected $_menu;
22 
26  protected $_iteratorFactory;
27 
32  public function __construct(
33  \Magento\Backend\Model\Menu\Filter\IteratorFactory $iteratorFactory,
34  \Magento\Backend\Model\Menu\Config $menuConfig
35  ) {
36  $this->_menu = $menuConfig->getMenu();
37  $this->_iteratorFactory = $iteratorFactory;
38  }
39 
43  public function toOptionArray()
44  {
45  $options = [];
46  $this->_createOptions($options, $this->_menu);
47  return $options;
48  }
49 
56  protected function _getMenuIterator(\Magento\Backend\Model\Menu $menu)
57  {
58  return $this->_iteratorFactory->create(['iterator' => $menu->getIterator()]);
59  }
60 
69  protected function _createOptions(&$optionArray, \Magento\Backend\Model\Menu $menu, $level = 0)
70  {
71  $nonEscapableNbspChar = html_entity_decode('&#160;', ENT_NOQUOTES, 'UTF-8');
72  $paddingString = str_repeat($nonEscapableNbspChar, $level * 4);
73 
74  foreach ($this->_getMenuIterator($menu) as $menuItem) {
76  if ($menuItem->getAction()) {
77  $optionArray[] = [
78  'label' => $paddingString . $menuItem->getTitle(),
79  'value' => $menuItem->getId(),
80  ];
81 
82  if ($menuItem->hasChildren()) {
83  $this->_createOptions($optionArray, $menuItem->getChildren(), $level + 1);
84  }
85  } else {
86  $children = [];
87 
88  if ($menuItem->hasChildren()) {
89  $this->_createOptions($children, $menuItem->getChildren(), $level + 1);
90  }
91 
92  $optionArray[] = ['label' => $paddingString . $menuItem->getTitle(), 'value' => $children];
93  }
94  }
95  }
96 }
__construct(\Magento\Backend\Model\Menu\Filter\IteratorFactory $iteratorFactory, \Magento\Backend\Model\Menu\Config $menuConfig)
Definition: Page.php:32
$children
Definition: actions.phtml:11
_getMenuIterator(\Magento\Backend\Model\Menu $menu)
Definition: Page.php:56