Drupal 7 – Check if a path exists

Keep working on the BeansTag module. One suggestion is to validate if the path exists when submitting a new BeansTag.

We can use drupal_valid_path() to check if a Drupal system URL exists.

if (drupal_valid_path('node/1')) {
  // url exists
} else {
  // url does not exists
}

 

According to the specification, this function allows dynamic URL checking like

if (drupal_valid_path('user/*', TRUE)) {
  // url exists
} else {
  // url does not exists
}

 

But the above code DOES NOT work at all. For more information, please refer to the following thread.
drupal_valid_path fails for dynamic paths (e.g. user/% cannot be added to menus)

Another thing you need to pay attention is that this function returns FALSE for path alias. So if you want to validate a URL for both system URL as well as path alias. You may need the following piece of code.

if (!drupal_valid_path($path)) {
  // Not a system URL.
  if (!drupal_lookup_path('source', $path)) {
    // Not a path alias.
    // $path does not exits
  }
}

 

Done =)

Reference:

Advertisements

One thought on “Drupal 7 – Check if a path exists”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s