[Navigation module] Losing breadcrumbs information when moving away from pages listed in navigation

edited August 2014 in Modules
Been tackling this for hours now, though I bet the solution to this is really easy...

I'd like to have breadcrumbs appear on every page in a site. The following code resides in blocks/header.php, and works fine with all pages that are listed under "Navigation" in the CMS.

<?php $nav_params = array( "group_id" => 'main', "parent_id" => uri_segment(1), "render_type" => "breadcrumb", "delimiter" => " &raquo; ", "container_tag_id" => "nav_breadcrumbs", "home_link" => "Pradžia" ); if (isset($breadcrumbs_append)) { $nav_params['append'] = $breadcrumbs_append; } echo fuel_nav($nav_params); ?>

The thing is, once I edit the URL at least slightly, some kind of information is lost and I only see "Pradžia" (the home link), nothing else.

For example, there's a news archive page that is accessible via "news/archive". I have a "News" controller with an "archive" method for this. However, once the user selects a month and the url changes to, say, "news/archive/2014/09", the breadcrumbs go from "Home" > "News" > "Archive", to only "Home".

I have tried using the $nav array under "_variables". I've also tried using the 'append' parameter (hence the $breadcrumbs_append variable in the code), but the breadcrumbs wouldn't change no matter what.

I realised this is probably due to 'fuel_mode' being set to 'auto', and so set it to 'views'. However, I want to retain functionality like setting up the primary navigation via the CMS, etc.

I bet there is something really obvious I'm missing, and would really appreciate any help. Work on the site is going great apart from this simple breadcrumbs issue :)

Comments

  • edited 12:14AM
    Do you want the menu item to stay at "Home" > "News" > "Archive" for those pages or do you want it to be "Home" > "News" > "Archive" > "2014" > "09"... etc ? If you want the former, you can use the "selected" or "active" (alias) parameter for the menu item and set it to ":children" like so:
    $nav['news/archive'] = array('label' => 'Archive', 'parent_id' => 'news', 'active' => ':children');
  • edited August 2014
    Thanks for the reply.

    Do you want the menu item to stay at "Home" > "News" > "Archive" for those pages...
    Thing is, they aren't pages. The "archive" method in the "News" controller accepts the date as parameters, and retrieves relevant records from the News module.

    I tried adding the line you posted but the breadcrumbs didn't budge :(

    public function archyvas($year = null, $month = null, $day = null) { $vars['news'] = // ... logic to get relevant news $this->fuel->pages->render('naujienos/archyvas', $vars); } // Linked from the "action" param of a form containing a dropdown with all dates public function archyvas_search() { if (isset($_POST['year_month'])) { $format = "Y-m"; $date = DateTime::createFromFormat($format, $_POST['year_month']); if ($date != false) { list($year, $month) = explode('-', $_POST['year_month']); redirect(sprintf("naujienos/archyvas/%04d/%02d", $year, $month)); } } redirect("naujienos/archyvas"); }

    There is no need to change the fuel_mode to 'view' in the configuration file for this, yes? As there's no record for 'news/archive/2014/09' in the CMS, it probably *has* to look at the $nav array.

    ... or do you want it to be "Home" > "News" > "Archive" > "2014" > "09"... etc ?
    Would that be difficult to implement? I will most likely have to do it this way in my next project.
  • edited 12:14AM
    The navigation doesn't look at pages in the CMS to render, but either the $nav array or the Navigation module if there are any records in there. What does your current $nav array look like (or at least the relevant parts concerning the home, news, archive pages)?
  • edited 12:14AM
    It is empty. I tried adding the line you wrote but it did not affect the breadcrumbs.
  • edited 12:14AM
    Are you using the Navigation module in the CMS or the $nav array to render the breadcrumb?
  • edited 12:14AM
    Just the navigation module in the CMS. The 'nav.php' file is empty at this point.
  • edited August 2014
    If you are using the Navigation module, the parent_id is actually the "id" record value and not the uri_segment. So you'll need to do something like the following to get that record first:
    $nav_record = $this->fuel_navigation_model->find_by_location(uri_segment(1), 'main'); $nav_params = array( "group_id" => 'main', "parent_id" => $nav_record['id'], "render_type" => "breadcrumb", "delimiter" => " &raquo; ", "container_tag_id" => "nav_breadcrumbs", "home_link" => "Pradžia" );
  • edited August 2014
    Thanks admin. I edited my header.php file but nothing seemed to change.

    header.php
    <?php $nav_record = $this->fuel_navigation_model->find_by_location(uri_segment(1), 'main'); echo fuel_nav(array( "group_id" => 'main', "parent_id" => $nav_record['id'], "render_type" => "breadcrumb", "delimiter" => " &raquo; ", "container_tag_id" => "nav_breadcrumbs", "home_link" => "Pradžia" )); ?>

    I added a 'var_dump' of $nav_record just before outputting the fuel_nav.

    Both times the var_dump output is exactly the same, yet as you can see the breadcrumb is not displayed in full in the latter case.
  • edited 12:14AM
    Of these breadcrumb items, "Home" > "News" > "Archive" > "2014" > "09", which are in the CMS in the navigation module? Those that aren't in the CMS will need to either be added (e.g. "Archive"), or dynamically appended (e.g. 2014, 09). If one of those menu items doesn't exist, then the parent/child chain is broken and the menu won't render. So in this case, we'll be mixing menu items found in the CMS, which use numeric ID indexes, with menu items that use string based indexes. This is OK to do as long as the parent_id/id relationships remain in tact.

    So say for example, you have Home, News, and Archive in the CMS. Each of those will have numeric indexes and would output an array something like the following:
    array(0 => array('id' => 1, 'group_id' => 1, 'label' => 'Home', 'location' => 'home', 'parent_id' = 0, ....), array(1 => array('id' => 2, 'group_id' => 1, 'label' => 'News', 'location' => 'news', 'parent_id' = 0,....) array(2 => array('id' => 3, 'group_id' => 1, 'label' => 'Archive', 'location' => 'news/archive', 'parent_id' => 1....);
    Note the parent_id and the id values (the group_id will be the numeric value found in the CMS but the fuel_nav function accepts either the string or numeric index). To append to the "Archive" menu item, you'll need to use it's parent_id value like so:
    $append = array( 'news/archive/2014' => array('label' => '2014', 'location' => 'news/archive/2014', 'parent_id' => 2), // note that 2 is the id of the Archive menu item in the CMS 'news/archive/2014/09' => array('label' => '2014', 'location' => 'news/archive/2014', 'parent_id' => 'news/archive/2014') );
    The 'id' values for the $append array will automatically use the array keys unless an "id" parameter is explicitly stated.
  • edited 12:14AM
    Yes, that's it! I didn't realise it was looking at parent_id's recursively like that, it all makes sense now.

    I finally have a working solution, and will deploy the site tonight. Thank you very much!

    As for your first question, the ones in Navigation were "News" and its child "Archive" :)
Sign In or Register to comment.