I'm looking for a way to determine where the users are coming from to build the breadcrumb for a page.
Is there something I can do with hook_breadcrumb_alter(&$active_trail, $item)
, or should I use the Flag module or any activity tracker module?
Drupal Answers is a question and answer site for Drupal developers and administrators. It only takes a minute to sign up.
Sign up to join this communityI'm looking for a way to determine where the users are coming from to build the breadcrumb for a page.
Is there something I can do with hook_breadcrumb_alter(&$active_trail, $item)
, or should I use the Flag module or any activity tracker module?
If you have control over the Urls linking to your target page, append a page-specific parameter. Suppose the page you are linking to is http://www.example.com/target-page
:
http://www.example.com/target-page?referer=page-1
http://www.example.com/target-page?referer=page-2
Then in your hook_menu_breadcrumb_alter
function, the referer will be available in the $_GET
variable, and you will be able to do something like this in the template.php
file for your theme:
function mytheme_breadcrumb(&$variables) {
if (request_uri() == '/target-page') {
switch($_GET['referer']) {
case 'page-1':
$variables['breadcrumb'][] = l('First referring page', '/page-1');
break;
case 'page-2':
$variables['breadcrumb'][] = l('Second referring page', '/page-2');
break;
}
}
}
If you don't want to add parameters to the Url, and are not using HTTPS, you could use the approach above except with $_SERVER['HTTP_REFERER']
in the switch
statement. Or alternatively, you could set a cookie on the referring pages and read it on the target page.
The example above is probably the easiest way to manipulate breadcrumbs but strictly speaking, there should not be business logic in a theme function. See these questions for more detailed info on how to manipulate breadcrumbs: