fuel_nav: Appending Categories Not Working

edited January 2016 in Modules
I have been reading through old forum posts (this and this and this) to figure out what I want to do. It's actually simple, I think, I just don't understand exactly how to do it.

On all pages made with the CMS, I have a sidebar menu for navigating each page's children. This sidebar menu is stored like this in /_variables/global.php:
$vars['sidemenu'] = fuel_nav(array('container_tag_id' => 'sidemenu', 'parent' =>uri_segment(1)));

I created a simple News module and I created a page (/news) with the CMS to output the data from the News module. The news page uses a special layout and also has its own _variables file.

I would like to create a special instance of the sidebar menu for use on the News page which links to Categories instead of the page's children. To do this, in /_variables/news.php I have put the following:

$all_cats = fuel_model('categories', array('select' => 'slug, name')); foreach($all_cats as $ac){ $news_nav[$ac->name] = array('parent_id'=> 'news', 'location' => 'news/'.$ac->slug, 'label' => $ac->name); } $vars['sidemenu'] = fuel_nav(array('container_tag_id' => 'sidemenu', 'append' => $news_nav));

This, however, outputs empty. I have tried other things as well, like using the $nav or $append variables. It's more a matter of me not getting how to do it than anything. I did read about fuel_nav() here http://docs.getfuelcms.com/helpers/fuel_helper#func_fuel_nav but that did not clarify for me.

Ultimately what I would want is for the news page to have each category listed in the sidebar menu, and then, after I solve that (simple) issue, on the category's page (news/category-slug), I would like a breadcrumb to trace back like Home > News > Category-Slug.

Any help is appreciated!

Comments

  • edited 9:11PM
    I think you are on the right track here. Try using 'news/'.$ac->slug for the key instead of $ac->name like this:
    all_cats = fuel_model('categories', array('select' => 'slug, name')); foreach($all_cats as $ac){ $news_nav['news/'.$ac->slug] = array('parent_id'=> 'news', 'location' => 'news/'.$ac->slug, 'label' => $ac->name); } $vars['sidemenu'] = fuel_nav(array('container_tag_id' => 'sidemenu', 'append' => $news_nav));
    The array key is the unique identifier that is used when doing the child/parent relationship.
  • edited January 2016
    Thanks for the super fast response! I did as you said and then it rendered only the pages created via the CMS, not the categories. However, I modified it slightly and changed:

    $news_nav['news/'.$ac->slug] = array('parent_id'=> 'news'

    to:

    $news_nav['news/'.$ac->slug] = array('parent'=> 'news'

    Now the side menu IS showing up including all of the pages made with the CMS and then, at the end, all of the categories. Any idea how to hide all things except the categories? I added 'root' => 'news' to $vars['sidemenu'] and it did hide all of the links I wanted to hide, but then it added a dead "News" link to the beginning of my side menu.

    Any ideas?
  • edited 9:11PM
    Continue to use parent_id. Also try adding the "active" parameter to the to the 'news' nav item in your nav.php file:
    $nav['news'] = array(.... 'active'=> ':children');
  • edited January 2016
    I changed parent back to parent_id and added this to /_variables/nav.php but nothing resulted from it:
    $nav['news'] = array('active' => ':children');
  • edited 9:11PM
    Sorry... after reading this again it sounds like you want the menu to start from the news area and all of it's children correct? If so, try adding the "parent" parameter to the fuel_nav function itself (not parent_id like for each menu item):
    $vars['sidemenu'] = fuel_nav(array('container_tag_id' => 'sidemenu', 'append' => $news_nav, 'parent' => uri_segment(1)));
  • edited 9:11PM
    You understand correctly. The structure is:
    • HOME
    • ABOUT
    • NEWS
      • Articles
      • Press Releases
      • Publications...
    Of these, HOME, NEWS, and ABOUT are all pages I made with the CMS, which are stored in the navigation panel of the admin area. Articles, Press, Pubs..etc. by contrast are categories which I would like to appear as children of NEWS. On the NEWS page, I only want to render the children, like...
    • Articles
    • Press Releases
    • Publications...
    I updated the code as per your suggestion, but still no luck.

    In /_variables/news.php I now have:
    $all_cats = fuel_model('categories', array('select' => 'slug, name')); foreach($all_cats as $ac){ $news_nav['news/'.$ac->slug] = array('location' => 'news/'.$ac->slug, 'label' => $ac->name); } $vars['sidemenu'] = fuel_nav(array('container_tag_id' => 'sidemenu', 'append' => $news_nav, 'parent' => uri_segment(1)));
  • edited 9:11PM
    Just to make sure, have you cleared the cache in FUEL. Usually the fuel_nav stuff is in a layout and changing layout content requires there to be a cache cleared or you logged in (if logged in, it doesn't use the cache so you see the freshest changes). Also, do you have any records in the Navigation module. If so, those will take precedence over the nav.php file.
  • edited January 2016
    I don't think I have any records in the Navigation module... would that be http://site.com/fuel/navigation? I cleared the cache to no avail. Not sure what could be wrong! But I did notice that you have been referring to a nav.php file. All of the code I have posted has not been in nav.php, it has been in news.php (/_variables/news.php). Would that make a difference?
    Thanks for your time helping me figure this out!
  • edited 9:11PM
    It looks like you are missing the parent_id for each $news_nav item. Also, the _variables/nav.php file is what is used by default for the fuel_nav function and you could try just adding to the existing $nav array that is there your categories:
    $all_cats = fuel_model('categories', array('select' => 'slug, name')); foreach($all_cats as $ac){ $nav['news/'.$ac->slug] = array('location' => 'news/'.$ac->slug, 'label' => $ac->name, 'parent_id' => 'news'); }
  • edited 9:11PM
    I got it to work! For reference to anyone in the future, here's what I did. After I got it to work, I moved the code out of /_variables/news.php to /_variables/global.php because I need the info to be available in multiple menus (footer, side, etc.):
    $all_cats = fuel_model('categories', array('select' => 'slug, name')); foreach($all_cats as $ac){ $append['news/'.$ac->slug] = array('location' => 'news/'.$ac->slug, 'label' => $ac->name, 'parent_id' => 16); } $vars['sidemenu'] = fuel_nav(array('container_tag_id' => 'sidemenu', 'parent' =>uri_segment(1), 'append' => $append));

    For whatever reason, the parent_id would not work unless I used the numeric ID (which I found by going to /fuel/navigation and then editing the news item -- the ID showed up in the URL like this: /fuel/navigation/edit/16

    Works just like I want!
    Thanks admin for all of the time you offered helping with this! :D
Sign In or Register to comment.