Skip to content

Instantly share code, notes, and snippets.

@leknoppix
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leknoppix/11408543 to your computer and use it in GitHub Desktop.
Save leknoppix/11408543 to your computer and use it in GitHub Desktop.
Menu with treebehavior
<?php
/* view */
<?php
echo $this->Menu->create($pages,
'',
'pages',
'view',
array(
0 => array('name' => 'Accueil',
'controller' => 'posts',
'action' => 'index'
),
),
array(
0 => array(
'name' => 'Contact',
'controller' => 'contacts',
'action' => 'index'
),
)
);
<?php
class MenuHelper extends AppHelper {
public $helpers = array('Html');
public function create($pages, $identifiant = 'level', $level = 1, $model = null, $action = null, $begin = null, $end = null) {
$res = "\n<ul";
if (!$level) {
$res .= " id=\"$identifiant$level\"";
}
$res .= ">";
if ($begin) {
foreach ($begin as $b) {
if (isset($b['url'])) {
$res .= "\n\t<li class=\"$identifiant" . "$level\">" . $this->Html->link($b['name'], $b['url']);
} else {
$res .= "\n\t<li class=\"$identifiant" . "$level\">" . $this->Html->link($b['name'], array('controller' => $b['controller'], 'action' => $b['action']));
}
}
}
foreach ($pages as $page) {
$res .= "\n\t<li class=\"$identifiant" . "$level\">" . $this->Html->link($page['Page']['name'], array('controller' => $model, 'action' => $action, 'id' => $page['Page']['id'], 'slug' => $page['Page']['slug']));
if (!empty($page['children'])) {
$res .= $this->create($page['children'], $identifiant, $level + 1, $model, $action);
}
$res .= "</li>";
}
if ($end) {
foreach ($end as $e) {
if (isset($e['url'])) {
$res .= "\n\t<li class=\"$identifiant" . "$level\">" . $this->Html->link($e['name'], $e['url']);
} else {
$res .= "\n\t<li class=\"$identifiant" . "$level\">" . $this->Html->link($e['name'], array('controller' => $e['controller'], 'action' => $e['action']));
}
$res .= "</li>";
}
}
$res .= "\n</ul>";
return $res;
}
}
<?php
public function index() {
$pages = $this->Page->find('threaded', array(
'conditions' => array('Page.menu_id' => 1),
'fields' => array(
'Page.id',
'Page.name',
'Page.slug',
'Page.parent_id'
),
'order' => array('Page.lft ASC'),
));
$this->set('pages', $pages);
$this->render('index');
}
@leknoppix
Copy link
Author

NOTE: Add the help on the tableController:

public $helpers = array('Menu');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment