0

I am trying to filter text and the result replace depends on the language of the node. The filter process callback function accepts $langcode argument but it always pass as "und". If I could access the Entity object to check it's langauge that would be perfect. I've tried to understand where the "und" value comes from and understand that it's from the field settings. Currently I'm getting the language code from the active $language object and in order to filter per language I set the filter cache to FALSE and I don't wanna do this.

How can I overcome this problem?

Thanks, Bnaya

2 Answers 2

2

The process callback is generally called from the check_markup() function.

That function blindly passes the $langcode value through to the process function, so it is up to the caller of the check_markup() function to send through the appropriate language.

You can see the list of functions that call check_markup() here.

The main one that you will encounter would be the text module's _text_sanitize() function, which is what formats the text for text fields that user input filters.

As you can see, it also passes through the language of the field.

Going up another level we have hook_field_load(), which also passes through the langcode.

Back again we get field_attach_load(), which calls hook_field_load() via _field_invoke_multiple().

All these are passing through langcode.

field_attach_load() is called by all these functions.

So basically you can scour the API site and see that is calling check_markup() in your case and see why there is no langcode being passed in.

Alternatively, you can install the devel module and put

dpm(debug_backtrace());

in your format process function.

Then load the page where your text is printing out and you will get a backtrace on the screen.

The backtrace will give you all the functions that have been called to get to your code. It shows you all the parameters that have been passed into function along the way.

This way you can more quickly isolate the point where the language becomes LANGUAGE_NONE (UND) - In theory :)

2
  • Thanks, So the right thing to do is to find the setting of the field and change it to English, am I right? Do you have any idea where do I set it?
    – Bnayal
    Apr 25, 2013 at 7:05
  • I don't have much experience with multilingual sites unfortunately, so I'm not of much help there. - Just to be clear, your site is multi-language? If not then und is actually the correct language. You will only have english if you have multiple languages and one of them is english. - Also, check out the other answer from kiamlaluno too, he speaks sense.
    – rooby
    Apr 25, 2013 at 7:09
2

Modules implementing input filters should never make assumptions about for which entity they are called to change the content. An input filter can be called in different contexts: A function that calls check_markup() could use it for changing a node body, but also any text that is not associated with an entity, or associated to an entity that is different from the entity associated with the current page.

That is the reason why the implementations of the input filter callbacks should just depend from the parameters they get. There is no way for them to know for which entity (if an entity is involved) they are called.
For example, a field could need to render its content using check_markup(), but the language used for the field is different from the language used for the node. (It could happen that the node's language is French, but the field doesn't have any content in French, and the default language is used.)

2
  • I know the right thing is to use the $langcode parameter but what if it comes 'und'? :-)
    – Bnayal
    Apr 25, 2013 at 7:09
  • It is normal to get LANGUAGE_NONE for a field. As far as I know, only if the field is translated, it can get a different value. I will expand my answer, and show which function decide the language value a field has.
    – apaderno
    Apr 25, 2013 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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