Community

No dropdowns in Filter Criteria in Views module when linked with custom table

I have attached a custom table to views module and define a filter criteria by exposing table fields. When i select "expose filter to visitors" No dropdown option is given. In fact the exposed filter fields created by views are just normal text fields. I read somewhere that it is because I am using views_handler_filter_string and I need to do following to accomplish this:

1: Create a subclass of views_handler_filter_in_operator 2: Implement get_value_options 3: Tell views about your new handler.

Here is my current implementation of code. three fields in database table are nid, product_type, product_make.

function mymodule_views_data() {

    $data['custom_table']['table']['group'] = t('Custom Table');

    $data['custom_table']['table']['base'] = array(

            'field' => 'nid',
            'title' => t('Custom Table'),
        'help' => t('Example table contains example content and can be related to nodes.'),
        'weight' => -10,
  );


  $data['custom_table']['table']['join'] = array(

      'node' => array(
      'left_field' => 'nid',
      'field' => 'nid',
    ),
  );

  $data['custom_table']['nid'] = array(
    'title' => t('Example content'),
    'help' => t('Some example content that references a node.'),

    'relationship' => array(
      'base' => 'node',
      'base field' => 'nid',

      'handler' => 'views_handler_relationship',
      'label' => t('Default label for the relationship'),
      'title' => t('Title shown when adding the relationship'),
      'help' => t('More information on this relationship'),
    ),
  );


  $data['custom_table']['product_type'] = array(
    'title' => t('Product Type'),
    'help' => t('Product type field.'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );

  $data['custom_table']['product_make'] = array(
    'title' => t('Product Make'),
    'help' => t('Product make field.'),
    'field' => array(
      'handler' => 'views_handler_field',
      'click sortable' => TRUE,
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => views_handler_filter_string',
    ),
    'argument' => array(
      'handler' => 'views_handler_argument_string',
    ),
  );
  return $data;
}

I tried few things but can not figure out the exact solution ca anyone please help me on this? Thanks

Comments

Yes, you need to create your

Yes, you need to create your own handler, as a subclass of views_handler_filter (I am not sure that views_handler_filter_in_operator is going to be any help in creating a dropdown - it's meant for sets of checkboxes, but it might be worth reading as a good example of how to override a filter class).

The key function to override is value_form(); That's where you'll supply the form field and define it as a 'select' type. I assume you know something about the forms API, but if not you'll need to read up on it to understand what this function should return.

Change 'views_handler_filter_string' in the data for your field to the name of your new class. Add an entry for the file which defines the class to your module's .info, like this:

files[] = mymodule.custom_filter.inc

Clear cache, so Drupal scans the file and knows where the class is located. Your dropdown should now appear in the exposed filters form.

overriding value_form

Thanks for your answer alfaguru. Can you please put some more light on overriding that value_form function (how), is it the only function we need to override in that entire class? Thanks

Yes, that's the only function

Yes, that's the only function you absolutely need to override. In that function you'll return the form element with its values (by modifying the form array which is passed in by reference). I can't tell you exactly how to do that as it depends on your precise needs but the simplest case will be something like:

<?php
function value_form(&$form, &$form_state ) {
$form['value'] = array(
 
'#type' => 'select',
 
'#title' => t('Value to filter on'),
 
'#options' => array('One' => 'one', 'Two' => 'two', 'Three' => 'three',),
);
}
?>

I haven't tested this so cannot guarantee it works but it ought to be a useful starting point. Refer to handlers that come with Views and other modules for more comprehensive examples.

nobody click here