0

In a drupal 7 website, I have a view that is based on a custom table. I have a status column in that custom table, which is type of integer and has 0 or 1 value, and in a view I want to add exposed filter with dropdown, with values somehow like below

$options[0] = 'No';
$options[1] = 'Yes';

So that a user can choose a value from dropdown and filter out data.

So in my views.inc file I have implemented hook_views_data(), then I have added the following code to make that field accessible in a view

$data['custom_users']['status'] = array(
      'title' => t('User Status'),
      'help' => t('User Status'),
      'field' => array(
        'click sortable' => TRUE,
      ), 
      'filter' => array(
        'handler' => 'views_handler_filter_numeric',
      ), 
      'sort' => array(
        'handler' => 'views_handler_sort',
      ),
  );

Now when I am trying to add exposed filter using this field I am not able to make it available as dropdown. I am only getting a textfield as exposed filter for this field.

Any help will be appreciated.

3
  • Have you checked this article? atendesigngroup.com/articles/… Commented Nov 3, 2020 at 8:21
  • @PatrickScheffer Hello. Thanks for your response. Yes I have checked that article, and method mentioned in it somehow works with actual field table, in my case I have only one custom table that stores form submissions, some simple values like name and other details. So there I have a status column that holds either 0 or 1 value. In views I was expecting to have drop down option, like in case of node you can have filter with Published & Unpublished options in dropdown.
    – Vik Durve
    Commented Nov 3, 2020 at 11:52
  • How does your views_handler_filter_numeric class look? Commented Nov 3, 2020 at 12:05

1 Answer 1

0

You can use hook_form_alter() for this. simply change the field type to select and add your options. here is my working code

// implements hook_form_alter
function project_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id === 'views_exposed_form') {
    $view = $form_state['view'];
    // add your view name in 'view_name'.
    if ($view->name === 'view_name') { 
      // Checking status field is exists or not.
      if (isset($form['status'])) {
        $form['status']['#type'] = 'select';
        $form['status']['#empty_value'] = '';
        $form['status']['#empty_option'] = 'All';
        $form['status']['#options'] = array('0' => 'No', '1' => 'Yes');
        $form['status']['#size'] = 1;
        $form['status']['#attributes'] = array('id' => 'status-list');
      }
    }
  }
}

hope this will work :).

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.