Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Topmenu Class Reference
Inheritance diagram for Topmenu:
Template IdentityInterface AbstractBlock DataObject BlockInterface

Public Member Functions

 __construct (Template\Context $context, NodeFactory $nodeFactory, TreeFactory $treeFactory, array $data=[])
 
 getHtml ($outermostClass='', $childrenWrapClass='', $limit=0)
 
 addIdentity ($identity)
 
 getIdentities ()
 
 getCacheKeyInfo ()
 
 getMenu ()
 
- Public Member Functions inherited from Template
 __construct (Template\Context $context, array $data=[])
 
 setTemplateContext ($templateContext)
 
 getTemplate ()
 
 setTemplate ($template)
 
 getTemplateFile ($template=null)
 
 getArea ()
 
 assign ($key, $value=null)
 
 fetchView ($fileName)
 
 getBaseUrl ()
 
 getObjectData (\Magento\Framework\DataObject $object, $key)
 
 getCacheKeyInfo ()
 
- Public Member Functions inherited from AbstractBlock
 __construct (\Magento\Framework\View\Element\Context $context, array $data=[])
 
 getJsLayout ()
 
 getRequest ()
 
 getParentBlock ()
 
 setLayout (\Magento\Framework\View\LayoutInterface $layout)
 
 getLayout ()
 
 setNameInLayout ($name)
 
 getChildNames ()
 
 setAttribute ($name, $value=null)
 
 setChild ($alias, $block)
 
 addChild ($alias, $block, $data=[])
 
 unsetChild ($alias)
 
 unsetCallChild ($alias, $callback, $result, $params)
 
 unsetChildren ()
 
 getChildBlock ($alias)
 
 getChildHtml ($alias='', $useCache=true)
 
 getChildChildHtml ($alias, $childChildAlias='', $useCache=true)
 
 getBlockHtml ($name)
 
 insert ($element, $siblingName=0, $after=true, $alias='')
 
 append ($element, $alias='')
 
 getGroupChildNames ($groupName)
 
 getChildData ($alias, $key='')
 
 getUiId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getJsId ($arg1=null, $arg2=null, $arg3=null, $arg4=null, $arg5=null)
 
 getUrl ($route='', $params=[])
 
 getViewFileUrl ($fileId, array $params=[])
 
 formatDate ( $date=null, $format=\IntlDateFormatter::SHORT, $showTime=false, $timezone=null)
 
 formatTime ( $time=null, $format=\IntlDateFormatter::SHORT, $showDate=false)
 
 getModuleName ()
 
 escapeHtml ($data, $allowedTags=null)
 
 escapeJs ($string)
 
 escapeHtmlAttr ($string, $escapeSingleQuote=true)
 
 escapeCss ($string)
 
 stripTags ($data, $allowableTags=null, $allowHtmlEntities=false)
 
 escapeUrl ($string)
 
 escapeXssInUrl ($data)
 
 escapeQuote ($data, $addSlashes=false)
 
 escapeJsQuote ($data, $quote='\'')
 
 getNameInLayout ()
 
 getCacheKeyInfo ()
 
 getCacheKey ()
 
 getVar ($name, $module=null)
 
 isScopePrivate ()
 
- Public Member Functions inherited from DataObject
 __construct (array $data=[])
 
 addData (array $arr)
 
 setData ($key, $value=null)
 
 unsetData ($key=null)
 
 getData ($key='', $index=null)
 
 getDataByPath ($path)
 
 getDataByKey ($key)
 
 setDataUsingMethod ($key, $args=[])
 
 getDataUsingMethod ($key, $args=null)
 
 hasData ($key='')
 
 toArray (array $keys=[])
 
 convertToArray (array $keys=[])
 
 toXml (array $keys=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 convertToXml (array $arrAttributes=[], $rootName='item', $addOpenTag=false, $addCdata=true)
 
 toJson (array $keys=[])
 
 convertToJson (array $keys=[])
 
 toString ($format='')
 
 __call ($method, $args)
 
 isEmpty ()
 
 serialize ($keys=[], $valueSeparator='=', $fieldSeparator=' ', $quote='"')
 
 debug ($data=null, &$objects=[])
 
 offsetSet ($offset, $value)
 
 offsetExists ($offset)
 
 offsetUnset ($offset)
 
 offsetGet ($offset)
 
- Public Member Functions inherited from BlockInterface
 toHtml ()
 

Protected Member Functions

 getCacheLifetime ()
 
 _columnBrake ($items, $limit)
 
 _addSubMenu ($child, $childLevel, $childrenWrapClass, $limit)
 
 _getRenderedMenuItemAttributes (\Magento\Framework\Data\Tree\Node $item)
 
 _getMenuItemAttributes (\Magento\Framework\Data\Tree\Node $item)
 
 _getMenuItemClasses (\Magento\Framework\Data\Tree\Node $item)
 
 getCacheTags ()
 
- Protected Member Functions inherited from Template
 _construct ()
 
 _toHtml ()
 
 getRootDirectory ()
 
 getMediaDirectory ()
 
- Protected Member Functions inherited from AbstractBlock
 _construct ()
 
 _prepareLayout ()
 
 _beforeToHtml ()
 
 _afterToHtml ($html)
 
 _toHtml ()
 
 _getNotFoundUrl ($route='', $params=['_direct'=> 'core/index/notFound'])
 
 getCacheTags ()
 
 getCacheLifetime ()
 
 _loadCache ()
 
 _saveCache ($data)
 
 _getSidPlaceholder ($cacheKey=null)
 
- Protected Member Functions inherited from DataObject
 _getData ($key)
 
 _underscore ($name)
 

Protected Attributes

 $identities = []
 
 $_menu
 
- Protected Attributes inherited from Template
 $_viewVars = []
 
 $_baseUrl
 
 $_jsUrl
 
 $_allowSymlinks
 
 $_filesystem
 
 $_template
 
 $templateEnginePool
 
 $_storeManager
 
 $_appState
 
 $directory
 
 $templateContext
 
 $pageConfig
 
 $resolver
 
 $validator
 
- Protected Attributes inherited from AbstractBlock
 $_design
 
 $_session
 
 $_sidResolver
 
 $_nameInLayout
 
 $_layout
 
 $jsLayout = []
 
 $_request
 
 $_urlBuilder
 
 $_eventManager
 
 $_frontController
 
 $_assetRepo
 
 $_viewConfig
 
 $_cacheState
 
 $_logger
 
 $_escaper
 
 $filterManager
 
 $_localeDate
 
 $inlineTranslation
 
 $_isScopePrivate = false
 
 $_scopeConfig
 
 $_cache
 
- Protected Attributes inherited from DataObject
 $_data = []
 

Additional Inherited Members

- Static Public Member Functions inherited from AbstractBlock
static extractModuleName ($className)
 
- Data Fields inherited from Template
const XML_PATH_TEMPLATE_ALLOW_SYMLINK = 'dev/template/allow_symlink'
 
- Data Fields inherited from AbstractBlock
const CACHE_GROUP = \Magento\Framework\App\Cache\Type\Block::TYPE_IDENTIFIER
 
const CACHE_KEY_PREFIX = 'BLOCK_'
 
- Static Protected Attributes inherited from DataObject
static $_underscoreCache = []
 

Detailed Description

Html page top menu block

@api

Since
100.0.2

Definition at line 20 of file Topmenu.php.

Constructor & Destructor Documentation

◆ __construct()

__construct ( Template\Context  $context,
NodeFactory  $nodeFactory,
TreeFactory  $treeFactory,
array  $data = [] 
)
Parameters
Template\Context$context
NodeFactory$nodeFactory
TreeFactory$treeFactory
array$data

Definition at line 52 of file Topmenu.php.

57  {
58  parent::__construct($context, $data);
59  $this->nodeFactory = $nodeFactory;
60  $this->treeFactory = $treeFactory;
61  }

Member Function Documentation

◆ _addSubMenu()

_addSubMenu (   $child,
  $childLevel,
  $childrenWrapClass,
  $limit 
)
protected

Add sub menu HTML code for current menu item

Parameters
\Magento\Framework\Data\Tree\Node$child
string$childLevel
string$childrenWrapClass
int$limit
Returns
string HTML code

Definition at line 173 of file Topmenu.php.

174  {
175  $html = '';
176  if (!$child->hasChildren()) {
177  return $html;
178  }
179 
180  $colStops = [];
181  if ($childLevel == 0 && $limit) {
182  $colStops = $this->_columnBrake($child->getChildren(), $limit);
183  }
184 
185  $html .= '<ul class="level' . $childLevel . ' ' . $childrenWrapClass . '">';
186  $html .= $this->_getHtml($child, $childrenWrapClass, $limit, $colStops);
187  $html .= '</ul>';
188 
189  return $html;
190  }
_columnBrake($items, $limit)
Definition: Topmenu.php:130

◆ _columnBrake()

_columnBrake (   $items,
  $limit 
)
protected

Building Array with Column Brake Stops

Parameters
\Magento\Backend\Model\Menu$items
int$limit
Returns
array|void
Todo:
: Add Depth Level limit, and better logic for columns

Definition at line 130 of file Topmenu.php.

131  {
132  $total = $this->_countItems($items);
133  if ($total <= $limit) {
134  return;
135  }
136 
137  $result[] = ['total' => $total, 'max' => (int)ceil($total / ceil($total / $limit))];
138 
139  $count = 0;
140  $firstCol = true;
141 
142  foreach ($items as $item) {
143  $place = $this->_countItems($item->getChildren()) + 1;
144  $count += $place;
145 
146  if ($place >= $limit) {
147  $colbrake = !$firstCol;
148  $count = 0;
149  } elseif ($count >= $limit) {
150  $colbrake = !$firstCol;
151  $count = $place;
152  } else {
153  $colbrake = false;
154  }
155 
156  $result[] = ['place' => $place, 'colbrake' => $colbrake];
157 
158  $firstCol = false;
159  }
160 
161  return $result;
162  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$count
Definition: recent.phtml:13
$items

◆ _getMenuItemAttributes()

_getMenuItemAttributes ( \Magento\Framework\Data\Tree\Node  $item)
protected

Returns array of menu item's attributes

Parameters
\Magento\Framework\Data\Tree\Node$item
Returns
array

Definition at line 293 of file Topmenu.php.

294  {
295  $menuItemClasses = $this->_getMenuItemClasses($item);
296  return ['class' => implode(' ', $menuItemClasses)];
297  }
_getMenuItemClasses(\Magento\Framework\Data\Tree\Node $item)
Definition: Topmenu.php:305

◆ _getMenuItemClasses()

_getMenuItemClasses ( \Magento\Framework\Data\Tree\Node  $item)
protected

Returns array of menu item's classes

Parameters
\Magento\Framework\Data\Tree\Node$item
Returns
array

Definition at line 305 of file Topmenu.php.

306  {
307  $classes = [];
308 
309  $classes[] = 'level' . $item->getLevel();
310  $classes[] = $item->getPositionClass();
311 
312  if ($item->getIsCategory()) {
313  $classes[] = 'category-item';
314  }
315 
316  if ($item->getIsFirst()) {
317  $classes[] = 'first';
318  }
319 
320  if ($item->getIsActive()) {
321  $classes[] = 'active';
322  } elseif ($item->getHasActive()) {
323  $classes[] = 'has-active';
324  }
325 
326  if ($item->getIsLast()) {
327  $classes[] = 'last';
328  }
329 
330  if ($item->getClass()) {
331  $classes[] = $item->getClass();
332  }
333 
334  if ($item->hasChildren()) {
335  $classes[] = 'parent';
336  }
337 
338  return $classes;
339  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17

◆ _getRenderedMenuItemAttributes()

_getRenderedMenuItemAttributes ( \Magento\Framework\Data\Tree\Node  $item)
protected

Generates string with all attributes that should be present in menu item element

Parameters
\Magento\Framework\Data\Tree\Node$item
Returns
string

Definition at line 277 of file Topmenu.php.

278  {
279  $html = '';
281  foreach ($attributes as $attributeName => $attributeValue) {
282  $html .= ' ' . $attributeName . '="' . str_replace('"', '\"', $attributeValue) . '"';
283  }
284  return $html;
285  }
$attributes
Definition: matrix.phtml:13
_getMenuItemAttributes(\Magento\Framework\Data\Tree\Node $item)
Definition: Topmenu.php:293

◆ addIdentity()

addIdentity (   $identity)

Add identity

Parameters
string | array$identity
Returns
void

Definition at line 347 of file Topmenu.php.

348  {
349  if (!in_array($identity, $this->identities)) {
350  $this->identities[] = $identity;
351  }
352  }

◆ getCacheKeyInfo()

getCacheKeyInfo ( )

Get cache key informative items

Returns
array
Since
100.1.0

Definition at line 370 of file Topmenu.php.

371  {
372  $keyInfo = parent::getCacheKeyInfo();
373  $keyInfo[] = $this->getUrl('*/*/*', ['_current' => true, '_query' => '']);
374  return $keyInfo;
375  }

◆ getCacheLifetime()

getCacheLifetime ( )
protected

Get block cache life time

Returns
int
Since
100.1.0

Definition at line 69 of file Topmenu.php.

70  {
71  return parent::getCacheLifetime() ?: 3600;
72  }

◆ getCacheTags()

getCacheTags ( )
protected

Get tags array for saving cache

Returns
array
Since
100.1.0

Definition at line 383 of file Topmenu.php.

384  {
385  return array_merge(parent::getCacheTags(), $this->getIdentities());
386  }

◆ getHtml()

getHtml (   $outermostClass = '',
  $childrenWrapClass = '',
  $limit = 0 
)

Get top menu html

Parameters
string$outermostClass
string$childrenWrapClass
int$limit
Returns
string

Definition at line 82 of file Topmenu.php.

83  {
84  $this->_eventManager->dispatch(
85  'page_block_html_topmenu_gethtml_before',
86  ['menu' => $this->getMenu(), 'block' => $this, 'request' => $this->getRequest()]
87  );
88 
89  $this->getMenu()->setOutermostClass($outermostClass);
90  $this->getMenu()->setChildrenWrapClass($childrenWrapClass);
91 
92  $html = $this->_getHtml($this->getMenu(), $childrenWrapClass, $limit);
93 
94  $transportObject = new \Magento\Framework\DataObject(['html' => $html]);
95  $this->_eventManager->dispatch(
96  'page_block_html_topmenu_gethtml_after',
97  ['menu' => $this->getMenu(), 'transportObject' => $transportObject]
98  );
99  $html = $transportObject->getHtml();
100  return $html;
101  }

◆ getIdentities()

getIdentities ( )

Get identities

Returns
array

Implements IdentityInterface.

Definition at line 359 of file Topmenu.php.

360  {
361  return $this->identities;
362  }

◆ getMenu()

getMenu ( )

Get menu object.

Creates \Magento\Framework\Data\Tree\Node root node object. The creation logic was moved from class constructor into separate method.

Returns
Node
Since
100.1.0

Definition at line 397 of file Topmenu.php.

398  {
399  if (!$this->_menu) {
400  $this->_menu = $this->nodeFactory->create(
401  [
402  'data' => [],
403  'idField' => 'root',
404  'tree' => $this->treeFactory->create()
405  ]
406  );
407  }
408  return $this->_menu;
409  }

Field Documentation

◆ $_menu

$_menu
protected

Definition at line 34 of file Topmenu.php.

◆ $identities

$identities = []
protected

Definition at line 27 of file Topmenu.php.


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