Views: Exposed filter If anonymous. Filter by user field if authenticated I have created two 'Postal code' (text) fields: one in a content type (Event) and another in the user account. I have also created a view showing Events. Now I would like to do the following:

When a user go to the events view:
  If the user is anonnymous a exposed filter must be show.
  If the user is authenticated, show only events with the postal code of the user.
Have you tried the module Views Dependent Filters?

For example, you could create a filter which checks if the user is logged in (or has a postal code) and set a dependency to that filter in your postal code filter.

You could also write some custom code (which might be easier in my opinion):

function YOUR_MODULE_form_alter(&amp;$form, $form_state, $form_id) {
  // Get the current logged in user object.
  global $user;

  // Check for the exposed form, you would do good to add
  // some extra checks to make sure you only identify your
  // postal code exposed filter.
  if($form_id == 'views_exposed_form') {
    // Load the full user object.
    $account = user_load($user-&gt;uid);

    // Check if the user has a postal code.
    if (!empty($account-&gt;field_postal_code[LANGUAGE_NONE][0]['value'])) {
      // Hide the exposed postal code field.
      $form['field_postal_code']['#type'] = 'hidden';
      // Set a default value for the exposed postal code field.
      $form['field_postal_code']['#default_value'] = $account-&gt;field_postal_code[LANGUAGE_NONE][0]['value'];
    }
  }
}


You might want to apply the filter immediately without needing to submit the form. You could use hook_views_pre_view for that:

function YOUR_MODULE_views_pre_view(&amp;$view, &amp;$display_id, &amp;$args) {
  global $user;

  if ($view-&gt;name == 'your_view') {
    // Do the same user postal code check as above here.

    // Apply the exposed filter.
    $view-&gt;exposed_input['field_postal_code'] = $account-&gt;field_postal_code[LANGUAGE_NONE][0]['value'];;
  }
}


I'm not sure if the hook_views_pre_views part works, though, so you might need to do a little research there.