1

I have created a new content type with a body and multiple fields.

I use the following query string

?article=referral-partners

to change the content of the node's body according to the value of the value of article.

The full URL looks like this (with my path alias I created):

http://localhost/userguides/contacts/my-first-user-guide?article=referral-partners

I would like to clean up the url to look like this instead:

http://localhost/userguides/contacts/my-first-user-guide/article/referral-partners

How can I do this with Drupal 7?

6
  • Where is the ?article=referral-partners coming from? Aug 12, 2014 at 23:20
  • My own custom block is generating it.
    – joshmmo
    Aug 12, 2014 at 23:33
  • Okay, so you're looking for "How can I use the cleaned up URL" rather than how to clean up the URL? Aug 12, 2014 at 23:41
  • uh yeah I guess that would be a better formed question sorry. I can generate my links in a cleaned form no problem, but how will I get that cleaned form as php variables?
    – joshmmo
    Aug 13, 2014 at 3:07
  • I think you can add a URL alias for userguides/contacts/my-first-user-guide?article=referral-partners to points to userguides/contacts/my-first-user-guide/article/referral-partners try it out here: admin/config/search/path/add Aug 13, 2014 at 6:52

3 Answers 3

0

Unless there's a chance your URL is going to change a lot as far as "depth" goes, you can use arg() to access any element in the path.

If your path is userguides/contacts/my-first-user-guide/article/referral-partners, you would use something like this...

$article = arg(4);     // $article is set to 'referral-partners'

... and then just check the value of $article. If the code can potentially be run on nodes or pages you don't want it to, then you'd also probably want to check other "args" for safety. Generally Drupal will let you tack on all kinds of extra path elements without needing to go through extra hoops as long as the initial path is a real one.

2
  • I had to use subautopath module to get it to work on alias URLs as well. But this is the approach I took. Thanks. Edit: Does drupal filter/sanitize this already?
    – joshmmo
    Aug 14, 2014 at 23:46
  • No sanitation involved in the function; it really just runs explode() on the path and then uses caching (drupal_static()) to avoid having to run explode() every time it's called. Aug 15, 2014 at 17:12
0

You can use something like this on your custom module for the redirects with the query strings.

/*
 * custom redirect with query string
 * module implements hook_init()
 */

function MyModule_init() {
  if ($_GET['q'] == 'userguides/contacts/my-first-user-guide/article/referral-partners') {
    drupal_goto('userguides/contacts/my-first-user-guide', array('query' => array('article' => 'referral-partners')));
  }
}
0

I was able to accomplish this by using the module Subpathauto

Instead of using query parameters like this: ?article=referral-partners I just use them like this /my-arg-i-want and I am able to get them in my module using arg(2).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.