Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Item.php
Go to the documentation of this file.
1 <?php
8 
11 
19 class Item
20 {
26  protected $_id;
27 
33  protected $_title;
34 
40  protected $_moduleName;
41 
47  protected $_sortIndex = null;
48 
54  protected $_action = null;
55 
61  protected $_parentId = null;
62 
68  protected $_resource;
69 
75  protected $_tooltip;
76 
82  protected $_path = '';
83 
89  protected $_acl;
90 
96  protected $_dependsOnModule;
97 
103  protected $_dependsOnConfig;
104 
110  protected $_submenu;
111 
115  protected $_menuFactory;
116 
120  protected $_urlModel;
121 
125  protected $_scopeConfig;
126 
130  protected $_validator;
131 
139 
145  protected $_moduleList;
146 
150  private $_moduleManager;
151 
157  private $target;
158 
169  public function __construct(
170  \Magento\Backend\Model\Menu\Item\Validator $validator,
171  \Magento\Framework\AuthorizationInterface $authorization,
172  \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
173  \Magento\Backend\Model\MenuFactory $menuFactory,
174  \Magento\Backend\Model\UrlInterface $urlModel,
175  \Magento\Framework\Module\ModuleListInterface $moduleList,
176  \Magento\Framework\Module\Manager $moduleManager,
177  array $data = []
178  ) {
179  $this->_validator = $validator;
180  $this->_validator->validate($data);
181  $this->_moduleManager = $moduleManager;
182  $this->_acl = $authorization;
183  $this->_scopeConfig = $scopeConfig;
184  $this->_menuFactory = $menuFactory;
185  $this->_urlModel = $urlModel;
186  $this->_moduleList = $moduleList;
187  $this->populateFromArray($data);
188  }
189 
198  protected function _getArgument(array $array, $key, $defaultValue = null)
199  {
200  return isset($array[$key]) ? $array[$key] : $defaultValue;
201  }
202 
208  public function getId()
209  {
210  return $this->_id;
211  }
212 
219  public function getTarget()
220  {
221  return $this->target;
222  }
223 
229  public function hasChildren()
230  {
231  return (null !== $this->_submenu) && (bool)$this->_submenu->count();
232  }
233 
239  public function getChildren()
240  {
241  if (!$this->_submenu) {
242  $this->_submenu = $this->_menuFactory->create();
243  }
244  return $this->_submenu;
245  }
246 
252  public function getUrl()
253  {
254  if ((bool)$this->_action) {
255  return $this->_urlModel->getUrl((string)$this->_action, ['_cache_secret_key' => true]);
256  }
257  return '#';
258  }
259 
265  public function getAction()
266  {
267  return $this->_action;
268  }
269 
277  public function setAction($action)
278  {
279  $this->_validator->validateParam('action', $action);
280  $this->_action = $action;
281  return $this;
282  }
283 
289  public function hasClickCallback()
290  {
291  return $this->getUrl() == '#';
292  }
293 
299  public function getClickCallback()
300  {
301  if ($this->getUrl() == '#') {
302  return 'return false;';
303  }
304  return '';
305  }
306 
312  public function getTitle()
313  {
314  return $this->_title;
315  }
316 
324  public function setTitle($title)
325  {
326  $this->_validator->validateParam('title', $title);
327  $this->_title = $title;
328  return $this;
329  }
330 
336  public function hasTooltip()
337  {
338  return (bool)$this->_tooltip;
339  }
340 
346  public function getTooltip()
347  {
348  return $this->_tooltip;
349  }
350 
358  public function setTooltip($tooltip)
359  {
360  $this->_validator->validateParam('toolTip', $tooltip);
361  $this->_tooltip = $tooltip;
362  return $this;
363  }
364 
372  public function setModule($module)
373  {
374  $this->_validator->validateParam('module', $module);
375  $this->_moduleName = $module;
376  return $this;
377  }
378 
386  public function setModuleDependency($moduleName)
387  {
388  $this->_validator->validateParam('dependsOnModule', $moduleName);
389  $this->_dependsOnModule = $moduleName;
390  return $this;
391  }
392 
400  public function setConfigDependency($configPath)
401  {
402  $this->_validator->validateParam('dependsOnConfig', $configPath);
403  $this->_dependsOnConfig = $configPath;
404  return $this;
405  }
406 
412  public function isDisabled()
413  {
414  return !$this->_moduleManager->isOutputEnabled(
415  $this->_moduleName
417  }
418 
424  protected function _isModuleDependenciesAvailable()
425  {
426  if ($this->_dependsOnModule) {
427  $module = $this->_dependsOnModule;
428  return $this->_moduleList->has($module);
429  }
430  return true;
431  }
432 
438  protected function _isConfigDependenciesAvailable()
439  {
440  if ($this->_dependsOnConfig) {
441  return $this->_scopeConfig->isSetFlag((string)$this->_dependsOnConfig, ScopeInterface::SCOPE_STORE);
442  }
443  return true;
444  }
445 
451  public function isAllowed()
452  {
453  try {
454  return $this->_acl->isAllowed((string)$this->_resource);
455  } catch (\Exception $e) {
456  return false;
457  }
458  }
459 
466  public function toArray()
467  {
468  return [
469  'parent_id' => $this->_parentId,
470  'module' => $this->_moduleName,
471  'sort_index' => $this->_sortIndex,
472  'dependsOnConfig' => $this->_dependsOnConfig,
473  'id' => $this->_id,
474  'resource' => $this->_resource,
475  'path' => $this->_path,
476  'action' => $this->_action,
477  'dependsOnModule' => $this->_dependsOnModule,
478  'toolTip' => $this->_tooltip,
479  'title' => $this->_title,
480  'target' => $this->target,
481  'sub_menu' => isset($this->_submenu) ? $this->_submenu->toArray() : null
482  ];
483  }
484 
492  public function populateFromArray(array $data)
493  {
494  $this->_parentId = $this->_getArgument($data, 'parent_id');
495  $this->_moduleName = $this->_getArgument($data, 'module', 'Magento_Backend');
496  $this->_sortIndex = $this->_getArgument($data, 'sort_index');
497  $this->_dependsOnConfig = $this->_getArgument($data, 'dependsOnConfig');
498  $this->_id = $this->_getArgument($data, 'id');
499  $this->_resource = $this->_getArgument($data, 'resource');
500  $this->_path = $this->_getArgument($data, 'path', '');
501  $this->_action = $this->_getArgument($data, 'action');
502  $this->_dependsOnModule = $this->_getArgument($data, 'dependsOnModule');
503  $this->_tooltip = $this->_getArgument($data, 'toolTip');
504  $this->_title = $this->_getArgument($data, 'title');
505  $this->target = $this->_getArgument($data, 'target');
506  $this->_submenu = null;
507  if (isset($data['sub_menu'])) {
508  $menu = $this->_menuFactory->create();
509  $menu->populateFromArray($data['sub_menu']);
510  $this->_submenu = $menu;
511  }
512  }
513 }
setConfigDependency($configPath)
Definition: Item.php:400
$title
Definition: default.phtml:14
setModuleDependency($moduleName)
Definition: Item.php:386
populateFromArray(array $data)
Definition: Item.php:492
_getArgument(array $array, $key, $defaultValue=null)
Definition: Item.php:198
$moduleManager
Definition: products.php:75
__construct(\Magento\Backend\Model\Menu\Item\Validator $validator, \Magento\Framework\AuthorizationInterface $authorization, \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig, \Magento\Backend\Model\MenuFactory $menuFactory, \Magento\Backend\Model\UrlInterface $urlModel, \Magento\Framework\Module\ModuleListInterface $moduleList, \Magento\Framework\Module\Manager $moduleManager, array $data=[])
Definition: Item.php:169