0

Im trying to get the content language abbreviation in a view, when I select "content: Language" field then it shows me "English" but I need to get its abbreviation "en", I though there was an option in the view field but there isn't.

  • Is that what's being stored or what's being rendered? Check the field displays. – rovr138 Nov 15 '16 at 22:59
  • In the views preview I see the word "English"... Is that what you mean? – SsouLlesS Nov 16 '16 at 15:54
  • I have checked a view that I render the language also. There is no option to display the abbreviation. I guess you can use Views Conditional and rewrite the result when the language is of certain string. – pierostz Nov 16 '16 at 15:59
2

Assuming this is the language of a node. It should actually be stored in the database as en, es, etc and then being rendered.

To get the content from the database without it being rendered here are the steps.

  • Go to your view. Here's a sample one I'm using:
  • Check the Information link on the bottom right hand side.

Views administration

  • When you click it, you should see a screen like so:

enter image description here

  • Here my two fields are listed as well as two other parts of the view where different things are occurring.
    The names on the right hand side are different overrides you can implement to intercept the process of generating the display. Depending on the name is the precedence. The further a name, the more specific it is.
  • Now, you need to decide how specific you want your change to be. I think using something like views-view-field--testlang--language.tpl.php is good. In my example, testlang is the name of the view and language the name of the field.
  • Click on the link next to the name of the field you want to override. It might be something like Field Content: Language (ID: language):. Once you have this, copy the content it's showing you.

Now that you have the name and the content of the field, go to your theme and create a file with the name you selected. If you have a templates folder on your theme, put it there.

Paste the code that your website generated for you,

<?php

/**
 * @file
 * This template is used to print a single field in a view.
 *
 * It is not actually used in default Views, as this is registered as a theme
 * function which has better performance. For single overrides, the template is
 * perfectly okay.
 *
 * Variables available:
 * - $view: The view object
 * - $field: The field handler object that can process the input
 * - $row: The raw SQL result that can be used
 * - $output: The processed output that will normally be used.
 *
 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}
 *
 * The above will guarantee that you'll always get the correct data,
 * regardless of any changes in the aliasing that might happen if
 * the view is modified.
 */
?>
<?php print $output; ?>

And now you modify it.

At the top you can see that you have the following variables available: $view, $field, $row, $output. The $row variable is the SQL result.
This is why I asked where you how it's stored on the database. I'm assuming up to now, we are using a node's language.

Well, if you take that <?php print $output; ?> and change it for <?php var_dump($row); ?>, it will give you the result from the database. If now you go to where you view is shown, or to the preview, you should see a line with something similar to:

object(stdClass)[94] public 'node_title' => string 'Consequat Nostrud Occuro Quadrum' (length=32) public 'nid' => string '26' (length=2) public 'node_language' => string 'es' (length=2) public 'node_created' => string '1479304373' (length=10).

Now, if you change your line to: <?php print $row->node_language; ?>, it should print the value as it's stored in the database in 2 letter country codes.

Result of the view on a Page display.

Good luck! Hope this helps.

  • Thanks for the time and effort in this answer, appreciated it. – SsouLlesS Nov 17 '16 at 20:32

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.