5

When I create a node in a non default language, I set an alias. After the node is saved, however, all internal links (e.g. menus) do not use the alias; they all use the node/[nid] link type, and when I navigate to the alias it returns the "page not found" error.

Aliases work fine for the nodes using the default language, and for language neutral nodes.

I enabled the Locale and the Content translation modules.

1 Answer 1

4

By default url alias created manually or by Pathauto works only for the language of the node.

But you could make it work for all languages ina few simple steps:

  • go to the page "admin/config/search/path",
  • find required url alias and click "edit"
  • select "All languages" in "Language" field and save

That's all, now this url alias will be active despite of what language has the user.

If manual change is not an option, you can:

  • try to use "neutral_paths" module (though it is in a sandbox mode)
  • create simple module that wil do the same

Module forces url aliases to be language neutral, it works both for pathtauto and manual path aliases:

<?php
/**
 * Implements hook_path_insert().
 */
function MYMODULE_path_insert($path) {
    _MYMODULE_path_language_fix($path['pid']);
}
/**
 * Implements hook_path_update().
 */
function MYMODULE_path_update($path) {
    _MYMODULE_path_language_fix($path['pid']);
}
/**
 * Fixing a given path id.
 */
function _MYMODULE_path_language_fix($pid) {
    db_update('url_alias')
        ->fields(array('language' => LANGUAGE_NONE))
        ->condition('pid', $pid)
        ->execute();
}
?>

If you already have lot of existing content with not correct languhae for aliases, then run this code

<?php
function _neutral_paths_set_all_to_neutral($type) {
    $num_updated = db_update('url_alias')
        ->fields(array('language' => LANGUAGE_NONE))
        ->condition('language', LANGUAGE_NONE, '!=')
        ->condition('source', $type . '/%', 'LIKE')
        ->execute();
    if ($num_updated > 0) {
        drupal_set_message(t('@num aliases were reset to language neutral', array('@num' => $num_updated)));
    }
    else {
        drupal_set_message(t('No aliases were updated.') . $type);
    }
}
_neutral_paths_set_all_to_neutral('node');
?>
2
  • To be honest doing this for each individual node is not a viable option. Commented Nov 26, 2014 at 4:38
  • I've updated my answer with example of the code that does it automatically Commented Nov 26, 2014 at 12:04

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.