1

I created view for fields from a custom table using handlers. I need to create a filter so that only the users who created the data can see the data. how can I create the filter?

0

We do not have token based filters, but instead what we can use is 'contextual filters', with in contextual filters you can get current logged in user.

I am assuming you are storing user information(mostly uid) to know who created what.

Now since its a custom table, the relation/join for users table with your custom table you have to define while exposing it to views. Here goes the sample code.

/**
 * Implements hook_views_data().
 */
function dev_test_views_data() {

  $data = array();

  $data['custom_view_expose']['table']['group'] = t('Custom table exposed');

  $data['custom_view_expose']['table']['base'] = array(
    'title' => t('custom table exposed'),
    'help' => t('Contains records we want exposed to Views.'),
  );
  $data['custom_view_expose']['record_id'] = array(
    'title' => t('Record ID'),
    'help' => t('The record ID.'),
    'field' => array(
      'handler' => 'views_handler_field_numeric',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
  );
  $data['custom_view_expose']['data'] = array(
    'title' => t('Data'),
    'help' => t('The Data.'),
    'field' => array(
      'handler' => 'views_handler_field',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_string',
    ),
  );
  // Allow views to join it with users table if needed while querying database.
  $data['custom_view_expose']['table']['join'] = array(
    'users' => array(
      'left_field' => 'uid',
      'field' => 'uid',
    ),
  );

  $data['custom_view_expose']['uid'] = array(
    'title' => t('User ID'),
    'help' => t('The record user ID.'),
    'field' => array(
      'handler' => 'views_handler_field_user',
    ),
    'sort' => array(
      'handler' => 'views_handler_sort',
    ),
    'filter' => array(
      'handler' => 'views_handler_filter_numeric',
    ),
    // Allow this field to show in relationship option.
    'relationship' => array(
      'base' => 'users',
      'field' => 'uid',
      'handler' => 'views_handler_relationship',
      'label' => t('Users'),
    ),
    // Get this field as contextual filter option.
    'argument' => array(
      'handler' => 'views_handler_argument_user_uid',
      'numeric' => TRUE,
      'validate type' => 'uid',
    ),
  );

  return $data;
}

The above code exposes a very simple table as in screenshot

enter image description here

Now while creating the views within your contextual filter options will see uid as on the fields for which you can pick value to come from current logged in user, and views will automatically filter down results. Adding views screenshot

enter image description here.

| improve this answer | |

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.