Jump to content

Output name from select field


asbjorn
 Share

Recommended Posts

Background

I have a home.php template in which I have a dropdown select box. From this dropdown I am able to choose between my menus. I have Menu Builder installed. I created a field type with the name show_menu and type Page. Then I set the Menu Builder folder as parent of selectable pages.

Back end / admin

It seems to work back end. All my menus are listed when I edit a page that has the home.php template.

Front end / home.php

Here is my problem. I am trying to get the name from the selected menu (from back end) to show in my code. For a while I was able to get the ID, then I got all 3 IDs (I have 3 menus to choose from).

The menu I want to show is named home-menu, labeled Home Menu.

In the following code I have included both a non-working line as line 16. And a working line as line 17. But line 17 is "manual" and not connected to the back end editing, which is what I am trying to do. So can anybody help me with the right code line to output the name for Home Menu?

(I tried to work with the ID, but got fatal errors. Those errors disappeared when I manually use line 17.)

<?php $homemenu = $modules->get('MarkupMenuBuilder');

$menuname = $page->show_menu->select_value;
$menuname = "home-menu";

$options = array(
   'has_children_class' => 'has_children',
   'current_class' => 'current',
   'menu_css_id' => $menuname,
   'menu_css_class' => 'nav',
);
echo $homemenu->render($menuname, $options);
?>

Btw. I am no PHP guru, I get by mostly through copy/paste simple snippets. Please ask me if I lack any details or am unclear on my problem. This is my first post, so please correct me if I am posting wrong or something.

Link to comment
Share on other sites

i don't get this:

$menuname = $page->show_menu->select_value;

if you want the name then it would be:

$menuname = $page->show_menu->name;

you could also setup a fallback in case you haven't made show_menu required, like:

$menuname = $page->show_menu->name ?: "home-menu";

oops - almost forgot: welcome to the forums, and to processwire! :)

Edited by Macrura
  • Like 1
Link to comment
Share on other sites

Thank for you reply, Macrura. I also tried what you suggested, but without any luck. I am not able to get the name with that code line. I only get the ID from $menuname, using this line:

$menuname = $page->show_menu;

The Menu Builder module page says I can render with the ID. But I do not get it to work with this line:

echo $homemenu->render($menuname, $options);

I get it to work with this:

echo $homemenu->render(1045, $options);

where 1045 is the ID for home-menu.

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...