Victor Sergienko
Victor Sergienko

Reputation: 13475

Drupal: hook_url_inbound_alter in url_rewrite: rewrite query part of URL for paging

I'm trying to use url_rewrite to work around nonfriendly parameters in URL of paging module. I wish to turn URLs like page-title.html?page=0,1 to page-title/page1.html.

Here are my hooks:

function mymod_url_outbound_alter(&$path, &$options, $original_path) {
    $localPath = $path . '?' . $options['query'];
    dpm("_url_outbound_alter($localPath)");
    if (preg_match('|(.+)\.html\?page=0%2C(\d+)|', $localPath, $matches)) {
        $path = "${matches[1]}/page${matches[2]}.html";
        unset($options['query']);
        dpm("altering path to $path");
    }
}

function mymod_url_inbound_alter(&$result, $path, $path_language) {
    if (preg_match('|(.+)/page(\d+)\.html|', $path, $matches)) {
        //$result = "${matches[1]}.html?page=0,${matches[2]}";
        $result = "${matches[1]}.html";
        //$_GET['q'] = "page=0,${matches[2]}";
        $_GET['page'] = "0,${matches[2]}";
        dpm("altering in-path to $result");
    }
}

function mymod_boot() {}

Is it impossible to add query part in hook_url_inbound_alter?

Yes, I know it's better to fix paging to use non-query URL. But the module is a little too complex to do that reliably. pagination module lacks features for me.

Is the problem in URL altering? What can I do to make it work?

Upvotes: 4

Views: 5960

Answers (1)

Oswald
Oswald

Reputation: 31647

$options['query'] is an array that contains the query parameters and their values, so you would have to do something like

$localPath = $path . '?' . $options['query']['page']

Also note that $path has not been rewritten by the Path module yet. Here is what works for me:

function pageing_url_outbound_alter(&$path, &$options, $original_path)
{
    if ($path == 'admin/content' && isset($options['query']['page']))
    {
        $path = 'admin/content/page' . $options['query']['page'];
        unset($options['query']['page']);
    }
}

function pageing_url_inbound_alter(&$path, $original_path, $path_language)
{
    if (preg_match('|admin/content/page(\d+)|', $path, $matches))
    {
        $path = 'admin/content';
        $_GET['page'] = $matches[1];
    }
}

This is for Drupal 7 RC3

Upvotes: 3

Related Questions