Safely Grabbing Drupal Path Args
In Drupal there are system paths (not to be confused with friendly URLs) that provide information that can be used to make a page or block "self-aware". In the example below, the user friendly path may have any one of the three example system paths.
Bad Assumption (unsafe)
<?php
$iNID = arg(1);
?>
This will fail if this logic lands on a tag page or a custom menu hook.
Proper Treatment: Node
<?php
if ((arg(0) == 'node') && (is_numeric(arg(1)))) {
$iNID = arg(1);
}
//when you are ready to start using $iNID, check it for !empty() before making use of it.
?>
No notices will be thrown because arg(1) is not evaluated at all if arg(0) is not a node.
Proper Treatment Term
<?php
if ((arg(0) == 'taxonomy') && (arg(1) == 'term') && (is_numeric(arg(2)))) {
$iTID = arg(2);
}
//when you are ready to start using $iTID, check it for !empty() before making use of it.
?>
No notices will be thrown because arg(1) is not evaluated at all if arg(0) is not a taxonomy and arg(2) is not evaluated if arg(1) is not term.