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

NancyDru’s picture

Isn't that what the Context module does?

juankvillegas’s picture

I 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.

NancyDru’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

No activity in 4 yrs.