Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Custom variable for menu


Go to End


15 Posts   3613 Views

Avatar
Devlin

Community Member, 344 Posts

7 July 2015 at 11:48pm

Which vars do you mean exactly?

e.g.
control Menu(1) would be something like this:

foreach ($this->Menu(1) as $item) {
	if ($item->LinkOrSection() == 'section' && $item->Children()) {
		// etc. pp.
	}
}

Avatar
Kamian

Community Member, 7 Posts

8 July 2015 at 12:42am

Edited: 08/07/2015 12:48am

Everytime I use "$this->Menu(1)" I get a Server Error.

All I want is to find out if the current page have children. But if I'm on home, the 4 links don't count.

Like this:

$page = $this;
$pages = array();
		
if($page->ShowInMenus || ($page->ID == $this->ID)) {
	$pages[] = $page;
}

$Parent = $pages[0]->record["ParentID"];

if($Parent != NULL && $Parent != 0 && !empty($Parent)) {
	$output = true;
}

Thats just reversed. There I have a "ParentID", poorly I don't have "ChildrenID".

Avatar
Devlin

Community Member, 344 Posts

8 July 2015 at 1:31am

Edited: 08/07/2015 1:48am

Everytime I use "$this->Menu(1)" I get a Server Error.

Please set you environment to 'dev' mode. Then we could get a proper error message.

Director:
  environment_type: 'dev'

I really don't see what you trying to accomplish with your code. I've abbreviated your code to something simple:

if (!empty($this->ParentID)) {
	$output = true;
}

There I have a "ParentID", poorly I don't have "ChildrenID".

You don't have a ChildrenID, but an ID. SilverStripe will do the rest for you.

e.g. if we have two pages:

- ParentPage // ID = 12
    - ChildPage // ID = 13

foreach($ParentPage->Children() as $child) {
     echo $child->ID // 13
     echo $child->ParentID // 12
}

$parent = $ChildPage->Parent();
echo $parent->ID // 12

Avatar
Devlin

Community Member, 344 Posts

8 July 2015 at 1:39am

All I want is to find out if the current page have children. But if I'm on home, the 4 links don't count.

// if the current page have children ...
if ($this->Children()) {
    $output = true;
}
// But if I'm on home, the 4 links don't count
if ($this->URLSegment == 'home') {
    $output = false;
}

Avatar
Kamian

Community Member, 7 Posts

8 July 2015 at 2:07am

Edited: 08/07/2015 2:12am

Well, sometimes I'm really stupid. Thats exactly what I'm searching for.

Just a little thing. If there's no children anymore then $output should be false. But thats the point where I don't know how I should do this.

Avatar
Devlin

Community Member, 344 Posts

8 July 2015 at 2:18am

Edited: 08/07/2015 2:28am

If there's no children anymore then $output should be false. But thats the point, I don't know how I should do this.

The condition "<% if HaveChilden %>open<% end_if %>" will only be executed if HaveChilden returns true. But this will only do so if current page has children.

// default state
$output = false;

// if the current page have children ...
// ... else $output keeps being false
if ($this->Children()->exists()) {
    $output = true;
}

// But if I'm on home, the 4 links don't count
// ... else $output is either true if it $this has children or keeps being false
if ($this->URLSegment == 'home') {
    $output = false;
}

return $output // true or false

edit
I've made an error. "if ($this->Children())" will always return true, please use "if ($this->Children()->exists())" instead.

Avatar
Kamian

Community Member, 7 Posts

8 July 2015 at 7:37pm

Thank you alot! That's exactly what I wanted. (:

Go to Top