[Navigation module] Losing breadcrumbs information when moving away from pages listed in navigation
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" => " » ",
"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
$nav['news/archive'] = array('label' => 'Archive', 'parent_id' => 'news', 'active' => ':children');
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.
$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" => " » ", "container_tag_id" => "nav_breadcrumbs", "home_link" => "Pradžia" );
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" => " » ", "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.
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.
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"