The Prepopulate module is only 65 lines of code. It does something really handy. It takes variables from the URL and uses them to fill in form fields. There are a couple ways this can be useful, like giving a link to a form that has some default value. Making bookmarklets is the other big use for this module. There are a couple examples in the Drupal handbook on Prepopulate, like this one I submitted:
So this is pretty cool. But there can be issues with long URLs. HTTP doesn't limit the length of URLs but browsers do. I don't mean the URL of the bookmarklet here. When the surfer clicks the bookmarklet, the browser will create and follow a URL like
http://example.com/node/add/customenode?edit[title]=title&edit[body_field][body]=body&edit[field_external_link][0][url]=currentsiteurl
The highlighted parts of that url are the parts that are filled in by the page the user is visiting when they click the bookmarklet. The rest of it is controlled by our site, so we can make it shorter. I'd like it to look more like:
http://example.com/r?t=title&s=body&u=currentsiteurl
Drupal will call a function named custom_url_rewrite_inbound() if that function exists. The function gets the inbound url path and a reference to the path Drupal will move on to. So instead of returning the path we want, the function should set the reference in $result. This isn't a hook but it's kind of like one. There's an issue open to make custom_url_rewrite_inbound() (and the corresponding custom_url_rewrite_outbound()) into hooks in Drupal 7. In the meantime just write your function in settings.php. Mine looks a lot like this:
This function's pretty easy to customize for any other site. The $path check ('r'), the $expand_vars array and the $dest path are all that need to be tweaked.
Right up at the top I check if this is the path '/r' since that's the only one I want to handle. This function gets called pretty often and usually the path will be something else so this quick check happens before any other work. Next we iterate through the URL parameters that we're interested in and, for each one, if it's set then we move it's value into the longer GET parameter that Prepopulate will recognize and fill in on a form for us.