I'm working on our breadcrumbs this morning, and would like to find the node ID of the referring page.

We are using url aliases, so for instance on this page: http://www.realself.com/laser_resurfacing_for_facial_skin_rejuvenation.html

I'd like to be able to see what node ID referred in, and provide that back to users as a breadcrumb.

I'm using referer_uri() - but that is giving me a full aliased path. I really need to distill that to a node ID so I can load the referring page title and set the breadcrumb correctly.

Any ideas on how to get just the referring nodeID?

Thanks,
-Greg

Comments

heine’s picture

drupal_get_normal_path.
--
Tips for posting to the forums.
When your problem is solved, please post a follow-up to the thread you started.

greg@beargroup.com’s picture

Used this code to set the breadcrumb to either the referring node or taxonomy. If its an external referral, I think it will show nothing.

 
	$refer = referer_uri();
	$alias = substr(strrchr($refer, "/"), 1);
	$norm_path = drupal_get_normal_path($alias);
	if (substr($norm_path, 0, 4) == "node") {
		$node_id = substr(strrchr($norm_path, "/"), 1);
		$refer_node = node_load($node_id);
		$breadterm = l($refer_node->title, $norm_path);
	} else if ( substr($norm_path, 0, 8) == "taxonomy")  {
		$term_id = substr(strrchr($norm_path, "/"), 1);
		$refer_term = taxonomy_get_term($term_id);
		$breadterm = l($refer_term->name, $norm_path);
	}
	drupal_set_breadcrumb(array(l(t('Home'), NULL), $breadterm));

nevets’s picture

If think the following code will also do what you want using the arg() function to get the parts of the path.

<?php
$data = arg(1);
switch ( $arg(0) ) {
case 'node':
  $refer_node = node_load($data);
  $breadterm = l($refer_node->title, $norm_path);
  break;

case 'taxonomy':
  $refer_term = taxonomy_get_term($data);
  $breadterm = l($refer_term->name, $norm_path);
}
 drupal_set_breadcrumb(array(l(t('Home'), NULL), $breadterm));
?>

You may want to check that $breadterm is set before calling drupal_set_breadcrumb().

greg@beargroup.com’s picture

Thanks for the idea, definitely looks cleaner!

Though arg(x) would only be calling to the currently in view URL, right? I am trying to parse the referring pages url. Would need to be able to set arg to use the referring page url instead of current page in order for that to work.

Yeah, if $breadterms is not set I can assume the referral is an external link and skip adding it - that's good. Thanks!

-Greg

nevets’s picture

Subject says it all

greg@beargroup.com’s picture

$refferingURI = explode('/', referer_uri());

And then can dissect the url like you would with arg(x).
$refferingURI[3] == arg(0)
$refferingURI[4] == arg(1)
... etc

finally looked in the path.inc file to see how real developers would do it :)
-G