开发者

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

开发者 https://www.devze.com 2023-01-31 05:00 出处:网络
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.

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?

  • If I comment out mymod_url_outbound_alter, it works, bot comma us URL-encoded - OK, it did show the friendly URL.
  • If I enable both, the page goes into infinite redirect 301 loop.
  • The commented out variants also don't seem to work.

Yes, I know it's better to fix paging to use non-query URL. But the mo开发者_JAVA技巧dule 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?


$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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号