Hello,

I have defined a custom_url_rewrite() function in my settings.php.
When going from source -> alias, it prefixes the language ('lang-en/' or 'lang-nl/') to the url and ...
when going from alias -> source, it strips the language from the url.

It works fine, except for one thing ... Some forms (including the login form :-/ ) redirect to a URL like
lang-nl/lang-nl/user/login?time=1155399481

The action attribute on the <form> element already contains the language prefix
and I guess after the form input is received, drupal_goto() is called on the destination url
again and custom_url_rewrite() then adds the prefix again ...

I think this is a bug, though I'm not entirely sure either whether I'm not doing something stupid myself ;-)
I tried to locate the exact cause of the problem, but it's taking me too much time :-/
So I'm posting the fragment from my settings.php below.
Hope someone can tell me what's going wrong.

Best regards,

Eric

if(preg_match('/^lang-en(\/|$)/', $_GET['q'])) {
  $GLOBALS['wkvlanguage'] = 'en';
} else {
  $GLOBALS['wkvlanguage'] = 'nl';
}

function custom_url_rewrite($type, $path, $original) {
  // $type is either 'alias' or 'source', depending on the
  // desired resulting path type of the operation (to be
  // in line with drupal_lookup_path() operation),
  // $path is possibly already processed by Drupal,
  // $original is the originally passed path

  global $wkvlanguage;

  $retval = $path;
  if ($path == $original) {
    // path is not yet aliased (this is the only case
    // conf_url_alias() was invoked in earlier versions)
    if ($type == 'source') {
      // Convert alias to real/source url.
      $retval = preg_replace('/^lang-(en|nl)\//', '', $retval);
      $retval = preg_replace('/^lang-(en|nl)$/', 'node', $retval);
    } else {
      // Convert real/source url to alias
      $retval = "lang-$wkvlanguage/$path";
    }
  }

  return $retval;
}


Comments

eveltman’s picture

I modified drupal_get_destination() to the following. It seems to work very well:

/**
 * Prepare a destination query string for use in combination with
 * drupal_goto(). Used to direct the user back to the referring page
 * after completing a form. By default the current URL is returned.
 * If a destination exists in the previous request, that destination
 * is returned.  As such, a destination can persist across multiple
 * pages. This function returns the URL in source form, NOT in alias form.
 * That's because the function result is usually used as 'destination'
 * parameter in the form's action URL. When the form input has been processed,
 * drupal_goto() is used with the value of the 'destination' URL parameter.
 * That function translates the URL to alias form before the actual redirect.
 *
 * @see drupal_goto()
 */
function drupal_get_destination() {
  if (isset($_REQUEST['destination'])) {
    return 'destination='. urlencode($_REQUEST['destination']);
  }
  else {
    // Important: We should /not/ use $_REQUEST here, because that
    // contains the URL in alias form. See function comments for
    // the reason why we don't want that.
    $path = isset($_GET['q']) ? $_GET['q'] : '';
    $query = drupal_query_string_encode($_GET, array('q'));
    if ($query != '') {
      $path .= '?'. $query;
    }
    return 'destination='. urlencode($path);
  }
}