Redirect to first available language when requested traduction (with Entity Translation) of a node does not exists

Public

Globalredirect provide such a function with the "Language path checking" option, but it does not work with Entity Translation for the moment (see https://www.drupal.org/node/1976320).
The patch did not work for me.

We use hook_node_view, to test if requested language is available in the translation of the node. If not, redirect to the first available language for this node.

</> CopyGet raw version
php
  1. /**
  2.  * Implements hook_node_view().
  3.  */
  4. function ifpek_ft_common_node_view($node) {
  5. // redirect to first available language when requested language is not available
  6. if (node_is_page($node)) {
  7. global $language;
  8. if (!array_key_exists($language->language, $node->translations->data)) {
  9. $languages = language_list();
  10. $available_languages = array_keys($node->translations->data);
  11. if (count($available_languages) > 0) {
  12. $first_lang = $languages[$available_languages[0]];
  13.  
  14. drupal_set_message(t('This content doesn\'t exist in the requested language.<br />
  15. <strong>You have been redirected to @language_native_name language.</strong>',
  16. array('@language_native_name' => $first_lang->native)));
  17.  
  18. $path = drupal_get_path_alias('node/' . $node->nid, $first_lang->language);
  19. drupal_goto($path, array('language' => $first_lang));
  20. }
  21. }
  22. }
  23. }