Menu Builder (ProcessWire)

Aus Wikizone
Version vom 15. April 2019, 14:01 Uhr von 37.49.72.8 (Diskussion) (Die Seite wurde neu angelegt: „ProcessMenuBuilder https://modules.processwire.com/modules/process-menu-builder/ https://gist.github.com/kongondo/a478e2a9274fc29f5d7cdb93a8166989 == Quick…“)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

ProcessMenuBuilder

https://modules.processwire.com/modules/process-menu-builder/
https://gist.github.com/kongondo/a478e2a9274fc29f5d7cdb93a8166989

Quickstart[Bearbeiten]

Menüs erstellen[Bearbeiten]

Setup > Menu Builder

Menüs im Frontend ausgeben[Bearbeiten]

// load the module
$menu = $modules->get('MarkupMenuBuilder');// $menu is an example
/**you can render by menu Page object, name, title, id or properly formatted array of menu items**/
// render by name, title or id
echo $menu->render('Title of Your Menu');// render the menu by title
echo $menu->render('name-of-your-menu');// render the menu by name
echo $menu->render('1234');//render by ID
// render by passing a Page object
$m = $pages->get(1234);
echo $menu->render($m);//render by Page object
// render by passing an array
// get the Menu Builder field menu_items for this menu. That is where your menu items JSON string is stored
$json = $pages->get(1234)->menu_items;
// convert the JSON string to an array. Here we assume the JSON string is not empty
$array = json_decode($json, true);
echo $menu->render($array);//render by array

You can render additional menus as well, e.g.

echo $menu->render('sidenav');

You can additionally pass CSS class/id options to the method. See above for available options.

$options = array(
	'has_children_class' => 'has_children',
	'current_class' => 'current',
	'menu_css_id' => 'main',
	'menu_css_class' => 'nav',
);
echo $menu->render('sidenav', $options);


render()[Bearbeiten]

render($menu, $options);
$menu ist ein Seitenobjekt oder der Titel, Name oder die ID eines Menüs
$options ist optional

Default Options[Bearbeiten]

$defaultOptions = array(
	'wrapper_list_type' => 'ul',// ul, ol, nav, div, etc.
	'list_type' => 'li',// li, a, span, etc.
	'menu_css_id' => '',// a CSS ID for the menu
	'menu_css_class' => '',// a CSS Class for the menu
	'submenu_css_class' => '',// CSS Class for sub-menus
	'has_children_class' => '',// CSS Class for any menu item that has children
	'first_class'=>'',// CSS Class for the first item in
	'last_class' => '',
	'current_class' => '',
	'default_title' => 0,// 0=show saved titles;1=show actual/current titles
	'include_children' => 4,// show 'natural' MB non-native descendant items as part of navigation
	'm_max_level' => 1,// how deep to fetch 'include_children'
	'current_class_level' => 1,// how high up the ancestral tree to apply 'current_class'
	'default_class' => '',// a CSS class to apply to all menu items
);

renderBreadcrumbs (Breadcrumb Menü)[Bearbeiten]

renderBreadcrumbs($menu, $options);

Breadcrumb Default Options[Bearbeiten]

$defaultOptions = array(
	'wrapper_list_type' => 'ul',// ul, ol, nav, div, etc.
	'list_type' => 'li',// li, a, span, etc.
	'menu_css_id' => '',
	'menu_css_class' => '',
	'current_css_id' => '',
	'current_class' => '',
	'divider' => '»',// e.g. Home >> About Us >> Leadership
	// prepend home page at the as topmost item even if it isn't part of the breadcrumb
	'prepend_home' => 0,// 0=no;1=yes
	'default_title' => 0,// 0=show saved titles;1=show actual/current titles
	'include_children' => 4,// show 'natural' MB non-native descendant items as part of navigation
	'b_max_level' => 1,// how deep to fetch 'include_children'
);

Komplexe Menüs mit getMenuItems()[Bearbeiten]

getMenuItems($menu, $type, $options);
// $type 1 will return an array and one of 2 (the default) will return a WireArray Menu 

Beispiele[Bearbeiten]

Breadcrumb[Bearbeiten]

Rendering Breadcrumbs Rendering breadcrumbs is quite similar to the above, the only difference being the method you use and some of the options that can be used to configure the output.

// load the module
$menu = $modules->get('MarkupMenuBuilder');// $menu is an example
/**you can render by menu Page object, name, title, id or properly formatted array of menu items**/
// render by name, title or id
echo $menu->renderBreadcrumbs('Title of Your Menu');// render the menu by title
echo $menu->renderBreadcrumbs('name-of-your-menu');// render the menu by name
echo $menu->renderBreadcrumbs('1234');// render by ID
// render by passing a Page object
$m = $pages->get(1234);
echo $menu->renderBreadcrumbs($m);
// render by passing an array
// get the Menu Builder field menu_items for this menu. That is where your menu items JSON string is stored
$json = $pages->get(1234)->menu_items;
// convert the JSON string to an array. Here we assume the JSON string is not empty
$array = json_decode($json, true);
echo $menu->renderBreadcrumbs($array);// render by array
Additionally, you can pass some options to the method. See above for available options.

$options = array(
	'wrapper_list_type' => 'div',
	'list_type' => 'span',
	//'list_type' => '',// if empty, no tag will be applied + no CSS ID
	'menu_css_id' => 'crumbs',
	'menu_css_class' => 'trail',
	'current_css_id' => 'current',
	'divider' => '*',
	'prepend_home' => 1
);
echo $menu->renderBreadcrumbs(1234, $options);