Jump to:

10931 Posts in 2614 Topics by 1810 members

All other Modules

SilverStripe Forums » All other Modules » Modifying "getMenu($level)" Function for Fallback Language Functionality

Discuss all other Modules here.

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

Page: 1
Go to End
Author Topic: 1078 Views
  • Ironcheese
    Avatar
    Community Member
    33 Posts

    Modifying "getMenu($level)" Function for Fallback Language Functionality Link to this post

    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

    1078 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.