0

I'm using Views 3 on Drupal 7 with the Computed Field module. My Computed Field calculates an int, and it is stored in the DB as an int, but Views is using the string handler by default.

How can I change the handler for one specific field from string to int?

This is the code I'm using in a custom module in mymodule.views.inc.

function mymodule_views_data_alter(&$data) {
  dpm($data);
  $data['user']['field_my_custom_field']['field']['handler'] = 'views_handler_field_numeric';
}

However, in my view, the filter choices are still:

  • Is equal to
  • Is not equal to
  • Contains
  • Contains any word
  • Starts with
  • etc.

These are the string options; I need them to be the choices for int (greater than, less than, etc.)

2
  • See the third example in the code in hook_views_data_alter(), that should get you 90% of the way... – Clive Apr 30 '15 at 8:38
  • Think I see what's wrong, I've put an answer in – Clive Apr 30 '15 at 9:35
1

You "should" just need to override the filter handler for the field, e.g.

function mymodule_views_data_alter(&$data) {
  $data['user']['field_my_custom_field']['filter']['handler'] = 'views_handler_filter_numeric';
}

Untested, use with caution!

0

In this case, I ultimately solved the problem by changing the type of my Computed Field from varchar to int-- I thought I had set it as int, but I was mistaken.

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.