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.

All other Modules /

Discuss all other Modules here.

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

Modifying "getMenu($level)" Function for Fallback Language Functionality


Go to End


1592 Views

Avatar
Ironcheese

Community Member, 36 Posts

1 June 2010 at 9:00am

Edited: 01/06/2010 10:22am

Hello everyone,

i need for my multilanguage site a modified Menu() Function because not all subpages are going to be translated. So i need a way to display the other pages if there are not available in the current selected language.

I managed to modify the getMenu() Function in the ContentController Class that it works on Level1 Navigation. The english Fallbacks works great... BUT for the deeper Levels it aint working...

Here my modified Function so far:

public function getMenu($level = 1) {
		if($level == 1) {
			$result = Translatable::get_by_locale("SiteTree", "en_US", "\"ShowInMenus\" = 1 AND \"ParentID\" = 0");

		} else {
			$parent = $this->data();
		
			$stack = array($parent);
			
			if($parent) {
				while($parent = $parent->Parent) {
						
					array_unshift($stack, $parent);
				}
			}
			
			if(isset($stack[$level-2])) $result = $stack[$level-2]->Children();
		}

		$visible = array();
		
		
		
		
		// Remove all entries the can not be viewed by the current user
		// We might need to create a show in menu permission
 		if(isset($result)) {
			foreach($result as $page) {
				
				
				if($page->canView()) {
					
					if($page->hasTranslation(Translatable::get_current_locale())){
						$page = $page->getTranslation(Translatable::get_current_locale());
						$visible[] = $page;
					}else{
						$page = $page->getTranslation('en_US');
						$visible[] = $page;
					}
					
				}
				
			}
			
		}
		
		return new DataObjectSet($visible);
	}

As i said, i need the functionality that subpages are displayed even if they are not available in the current selected language. So that visitors will have at least the change so see/read the english pages...

Thank you in advance, any help is appreciated =)

cheers,
Alex

PS: I'm not sure if i posted this in the right section :S

EDIT::
Ok, for some super über weird reason it suddenly stopped working -..-
As soon i switch to german i get follow User Warning:

[User Warning] DataObjectSet::__construct: Passed item #6 is not an object or associative array, can't be properly iterated on in templates

Edit No.2:
Ok, it works again if i delete my other Page Classes which i hade extended from the Page Class.

cheers