3

I have a View of taxonomy terms and for some reason I need to overwrite the terms' fields and link them "manually", using "Output this field as link". How can I process the '[name]' token through transliteration process before outputting it on the screen?

1

Here's a workaround that worked for me.

Prerequisites: Pathauto and Transliteration modules.

  1. Add a new text field to your taxonomy called "cleaned_name", or whatever you like. In a later step we will populate this field at view execution with the term name after passing it through pathauto_cleanstring.
  2. Add the "Taxonomy term: Name", and "Taxonomy term: cleaned_name" to your view. The "cleaned_name" field must be below the "Name" field.
  3. In the view settings for the "Name" field, mark the "exclude from display" checkbox.
  4. In the view settings for the "cleaned_name" field, click on "Rewrite results" to open the rewrite settings, then mark the "Rewrite the output of this field" checkbox.
  5. This will cause a textbox to appear, where you can build your link html. For example:

    <a href='PUT-YOUR-PATH-HERE/[field_cleaned_name]'>[name]</a>
    
  6. Next implement hook_views_pre_render. The idea is to take the the name, run it through pathauto_cleanstring, and store it in the cleaned_name field. I used the following code:

    function YOUR_MODULE_NAME_views_pre_render(&$view) {
        if ($view->name == "MACHINE_NAME_OF_YOUR_VIEW") {
           //dpm($view->result); INSTALL DEVEL MODULE AND UNCOMMENT THIS IF YOU NEED TO SEE WHAT FIELD NAMES TO USE
       module_load_include('inc','pathauto');
       foreach($view->result as $key => &$result) {
          $cleaned_name = pathauto_cleanstring($result->taxonomy_term_data_name);
      $result->field_field_cleaned_name[0]['rendered']['#markup'] =  $cleaned_name;
       } // End of FOREACH statement
       unset($result); 
         }
    }
    

Adding a field to your taxonomy just to have a place to return a transliterated version of your field is a little ugly. Another alternative I tried was to add the name field to the view twice, with the idea of running only one of them through pathauto_cleanstring, and thus having both a clean and uncleaned version available as tokens to build the link. This did not work however, because although you can add the term name field twice to a view, and views will create two tokens ([name], [name_1]), in the backend both tokens point to the same field. If we modify this field, both tokens will output the modified value.

Another alternative I tried was to build the link html inside the hook_views_pre_render function, and use that to overwrite the value in $result->taxonomy_term_data_name. That avoids the need to add a field to the taxonomy. However, the problem here is that views will escape the html when it displays it. I wasn't able to find a way around this.

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.