Search code examples
phpdrupalmenuinternationalization

Printing a custom menu in page.tpl.php in drupal 7 shows all nodes in all languages instead of only the current language


I am using the i18n module for drupal and I have two languages (eng and chinese). I have a main menu and a custom menu (contact menu) and a menu combining both. ALL menus are not in regions but are printed via page.tpl.

When I am on a node from the main menu and click on "chinese", the translated node is being shown and the main menu now only contains all the chinese node links - perfect!

Actually, the custom menu works the same, BUT only if I allocate it to a region. I have to print this menu via php code in my page.tpl - but then ALL nodes from this menu, english AND chinese, are printed. When I put the menu into a region though, only the current language's nodes are shown.

How can I render a custom menu to only show nodes in the current language?

Heres the php code (I am using/ writing a ZEN subtheme)

if ($main_menu): ?>
                <nav id="main-menu" role="navigation">  
                    <?php print theme(
                        'links__system_main_menu', array(
                            'links' => $main_menu,
                            'attributes' => array(
                                //'id' => 'main-menu-links',
                                'class' => array('links', 'main_menu', 'clearfix'),
                            )
                        )
                    ); ?>
                </nav>

            <?php
            endif;
?>

The above works great and only prints nodes/ links in the current language, unlike:

<?php if (menu_navigation_links('menu-contact-menu')): ?>
                <nav id="contact-menu" role="navigation">

                    <?php print theme(
                        'links', array(
                            'links' => menu_navigation_links('menu-contact-menu'), 
                            'attributes' => array(
                                'class'=> array('links', 'contact_menu')
                            )
                        )
                    ); ?>
                </nav>
            <?php
                endif;
            ?>

And how would the combinated menu look like? Currently:

<?php
            // combine main and contact menu and output as one
            if ($main_menu && menu_navigation_links('menu-contact-menu')): ?>
                <nav id="footer-menu" role="navigation">
                    <?php
                        $links = array_merge($main_menu, menu_navigation_links('menu-contact-menu'));

                        print theme(
                            'links', array(
                                'links' => $links, 
                                'attributes' => array(
                                    'class'=> array('links', 'footer-menu')
                                )
                            )
                        );
                    ?>
                </nav>
        <?php
            endif;
        ?>

Solution

  • found it out, kinda logical... i18n_menu_navigation_links does the trick

    if (menu_navigation_links('menu-contact-menu')): ?>
                    <nav id="contact-menu" role="navigation">
    
                        <?php print theme(
                            'links', array(
                                'links' => i18n_menu_navigation_links('menu-contact-menu', 0),
                                'attributes' => array(
                                    'class'=> array('links', 'contact_menu')
                                )
                            )
                        ); ?>
                    </nav>
                <?php
                    endif;
                ?>