Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'd like to have a function to know if an user is in an specific URL. For example, if I want to know if the user is seeing a page (Content Type), I'd use:
$args = arg();
if ($args[0] == 'node' && is_numeric($args[1]) &&count($args) == 2) { // The user is seeing a node.
$node = node_load($args[1]);
if ($node->type == 'page') { // The node is a page.
// HERE IS YOUR CODE
}
}
But I'd like to see something like:
if (util_check_url('node/%d')) {
$node = node_load(arg(1));
if ($node->type == 'page') {
// HERE IS YOUR CODE
}
}
I already have the function code, but I'd prefer to have it in Util module:
function util_check_url($pattern, $strict = FALSE) { // $strict is used to exactly match the URL (TRUE), or simply check the beginning of the URL (FALSE).
$args = arg();
$url = explode('/', $pattern);
$match = TRUE;
if (count($url) > count($args)) {
$match = FALSE;
}
elseif ($strict && (count($args) != count($url))) {
$match = FALSE;
}
else {
foreach ($url as $key => $value) {
switch ($value) {
case '%d': // Replacement for integers only
$match &= is_numeric($args[$key]);
break;
case '%s': // Replacement for any string
break;
default:
$match &= ($args[$key] == $value);
break;
}
}
}
return $match;
}
That's all.
Comments
Comment #1
NancyDruIsn't that what the Context module does?
Comment #2
juankvillegas CreditAttribution: juankvillegas commentedI hadn't seen Context module... it is very powerful, but it doesn't do what I want with this piece of code.
Context module works through Drupal UI... but this function would be useful in module (and maybe theme) development.
Comment #3
NancyDruNo activity in 4 yrs.