How to create custom contextual filter for views using views api.

I have a custom table which is having some statistics data about the user. The user id is also stored in the table.

I managed to get the results using views api, Now I would like to use the contextual filter on the logged-in user, this is where I got interrupted. The contextual links are empty, its not showing any filters I can use of. How do I get contextual filters working using views api?

1 Answer 1


did you implement hook_views_data and then defined you table there? if yes then did you define relation for uid? you can copy code from views/modules/node.views.inc

  // uid field
  $data['node']['uid'] = array(
    'title' => t('Author uid'),
    'help' => t('The user authoring the content. If you need more fields than the uid add the content: author relationship'),
    'relationship' => array(
      'title' => t('Author'),
      'help' => t('Relate content to the user who created it.'),
      'handler' => 'views_handler_relationship',
      'base' => 'users',
      'field' => 'uid',
      'label' => t('author'),
    'filter' => array(
      'handler' => 'views_handler_filter_user_name',
    'argument' => array(
      'handler' => 'views_handler_argument_numeric',
    'field' => array(
      'handler' => 'views_handler_field_user',
  • 1
    Below part play role for contextual filter. 'argument' => array( 'handler' => 'views_handler_argument_numeric', ),
    – Fawwad
    May 27, 2016 at 9:35

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.