2

I have a node type "news" with an included term reference field named "newscategory". i18n-module incl. taxonomy-i18n is also installed, vocabulary is translatable and all terms are translated. But for pathauto patterns there is only the token "[node:field_newscategory]" available, which will not be localized depending on the node language.

Is there any trick on achieving this?

1

Why don't you try Entity Token - it's part of the Entity module and provides additional tokens for use with i18n. It will make the token tree item for the term ref field expandable and you should see a token like node:field_newscategory:i18-name. This should get you going purely on the UI side.

Although I bet it rocks to be able to wrap one's own piece of code that just works :)

  • I would not curse this strong ;-) it's not been a wasted time. And after all - you just re-discovered the beauty of drupal.stackexchange.com ;-) – Artur Jan 21 '13 at 17:34
1

Okay, after 2 more hours of research and development I've finally managed to get the problem solved. I ended up writing a tiny custom module implementing hook_pathauto_alias_alter().

The module has several dependencies:

dependencies[] = taxonomy
dependencies[] = field_extract
dependencies[] = pathauto
dependencies[] = i18n
dependencies[] = i18n_string
dependencies[] = i18n_taxonomy

Here's how I did it (tailored to my special needs, so go on and modify my code)

<?php

  /**
   * Implements hook_pathauto_alias_alter()
   */
  function mymodule_pathauto_alias_alter( &$alias, array &$context ) {
    if ( $context[ 'module' ] == 'node' ) {
      $node = $context[ 'data' ][ 'node' ];
      if ( $node->type == 'news' ) {
        // determine actual context variables
        $language = $context[ 'language' ];
        $pattern = $context[ 'pattern' ];
        $category = field_extract_value( 'node', $node, 'field_newscategory' );

        // translation & preparation
        $category_translated = i18n_string_object_translate( 'taxonomy_term', $category, array( 'langcode' => $language ) );
        $sanitized = pathauto_cleanstring( $category_translated->name, array( 'langcode' => $language ) );

        // re-build the alias
        $alias = explode( '/', $alias );
        $pattern = explode( '/', $pattern );
        foreach ( $pattern AS $index => $part ) {
          if ( $part == '[node:field_newscategory]' ) {
            $alias[ $index ] = $sanitized;
            break;
          }
        }
        $alias = implode( '/', $alias );
      }
    }
  }

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.