Jump to content

Add a dynamic Language Switcher to Markup Simple Navigation Menu


Neo
 Share

Recommended Posts

I have a Menu that is generated using the "Markup Simple Navigation" module:

<?php // Echo Header Navigation Menu
$headerMenu = $modules->get("MarkupSimpleNavigation"); // load the module
          $options = array(
              // Show HOME in menu
              'show_root' => false,
              // Current class
              'current_class' => 'active',
              // Show parent class as active
              'parent_class' => 'active',
              // Top level ul
              'outer_tpl' => '<ul class="right">||</ul>',
              // set the max level rendered
              'max_levels' => 1,

          );
echo $headerMenu->render($options); // render Header menu ?>

I would like to add a language switcher as an li-element to the unordered list of the menu with the following markup:

<li class="has-dropdown">
   <a href="#">Right Button Dropdown</a>
     <ul class="dropdown">
       <li><a href="#">First link in dropdown</a></li>
       <li class="active"><a href="#">Active link in dropdown</a></li>
     </ul>
</li>

My language switcher is generated with the following:

<!-- language switcher / navigation -->
<li class="has-dropdown">
<a href="#">Right Button Dropdown</a>
<ul class="dropdown"><?php
     foreach($languages as $language) {
	if(!$page->viewable($language)) continue; // is page viewable in this language?
	if($language->id == $user->language->id) {
	     echo "<li class='active'>";
	} else {
	     echo "<li>";
	}
	$url = $page->localUrl($language); 
	$hreflang = $pages->get('/')->getLanguageValue($language, 'name'); 
	echo "<a hreflang='$hreflang' href='$url'>$language->title</a></li>";
	}
?></ul>
</li>

Would appreciate your advice how to add this to the option array of the menu.

Link to comment
Share on other sites

I would try to save your language switcher markup as a variable, and inject it via the outer_tpl option:

'outer_tpl' => "<ul>||$langSwitch</ul>",

// template string for the outer most wrapper. || will contain entries

-> http://modules.processwire.com/modules/markup-simple-navigation/

Not sure if that's possible. Of course you'd have to change your code above from echo() to $langSwitch .= "..." instead (delayed output)

Link to comment
Share on other sites

Thanks for your response.

I save the language switcher in a variable, which correctly displays when you echo it:

<!-- language switcher / navigation -->
<?php 
        $langSwitch = "";

        $langSwitch .="<li class=\"has-dropdown\"><a href=\"#\">Language</a><ul class=\"dropdown\">";

        foreach($languages as $language) {
          if(!$page->viewable($language)) continue; // is page viewable in this language?
          if($language->id == $user->language->id) {
            $langSwitch .="<li class=\"current\">";
          } else {
            $langSwitch .="<li>";
          }
          $url = $page->localUrl($language); 
          $hreflang = $pages->get('/')->getLanguageValue($language, 'name'); 
          $langSwitch .="<a hreflang=\"$hreflang\" href=\"$url\">$language->title</a></li>";
        }
        
        $langSwitch .="</ul></li>";
?>

However, I cannot get it display within the menu options array, which will literally print {$langSwitch}:

'outer_tpl' => '<ul class="right">||{$langSwitch}</ul>',

Any idea? 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...