0

I have a combined text box exposed filter of a view. Is there any way that I can alter the keywords used by the user to convert non-latin characters to latin using hook_views_query_alter?

1 Answer 1

0

Transliteration module has some useful functions to convert unicode strings into latin strings. You may give it a try and use some of those functions in your alter hooks. I cannot give you a specific function name but I'm sure that I've used this module for a similar purpose.

Anyway, if you work with non-latin languages, you'll probably need to install this module for other purposes, like fixing the filenames and url parts generated by user input etc.

A quick look-up into the source code of Transliteration module, I can say that you can use transliteration_get() function to transliterate a give string. A sample implementation for your case would be:

function YOUR_MODULE_views_query_alter(&$view, &$query) {
  // To debug comment out the following line (Devel module is required).
  //dpm($query);

  if ($view->name = 'YOUR_VIEW' && !empty($view->exposed_raw_input['YOUR_FIELD']) {
    $transliterated = transliteration_get($view->exposed_raw_input['YOUR_FIELD']);
    // Traverse through the 'where' part of the query.
    foreach ($query->where as &$condition_group) {
      foreach ($condition_group['conditions'] as &$condition) {
        // If this is the part of the query filtering on your field, change the
        // condition value to the transliterated string.
        if ($condition['field'] == 'YOUR FIELD') {
          $condition['value'] = $transliterated;
        }
      }
    }
  }
}
7
  • Yes it came to my mind that somehow I need to incorporate this module but I would love some specifics. Though reading through the readme file I guess the only function that I can use is this function_exists('transliteration_get')
    – pierostz
    Commented Oct 26, 2016 at 7:54
  • yes, I had a quick look into the source code, and I can say that you can use this function to transliterate a string.
    – Elin Y.
    Commented Oct 26, 2016 at 11:12
  • I've add a sample code to show how to use this function in a hook implementation
    – Elin Y.
    Commented Oct 26, 2016 at 11:25
  • I guess this is more that enough help I can get. Thanks. One more thing though...my search box is a combined filter. Which 'YOUR FIELD' I must use?
    – pierostz
    Commented Oct 26, 2016 at 11:49
  • I cannot know what fields are in your views defined. But you can use some debugging to find out the fields. If you don't have any debug setup like XDebug or so, I recommend the Devel module. After enabling Devel module, you can use dpm($query) to print out whole query object and inspect what conditions and fields are defined.
    – Elin Y.
    Commented Oct 26, 2016 at 14:06

Your Answer

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

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