14

This is a follow up question to Drupal Views exposed filter of Author name. The following question was answered and works. I can filter a view by user name. The user name is entered is entered by typing in a box and the box then auto completes. Rather then doing this I would like the list of users as a drop down. I only need one user to be selected. Do you know if this is possible?

Community
  • 1
  • 1
Linda
  • 2,107
  • 5
  • 29
  • 40

5 Answers5

25

You'll need a custom module for that.

I've done this for Drupal 7 this way: create a module, say, views_more_filters, so you have a views_more_filters.info file like this:

name = Views More Filters
description = Additional filters for Views.
core = 7.x

files[] = views_more_filters_handler_filter_author_select.inc
files[] = views_more_filters.views.inc

(file views_more_filters_handler_filter_author_select.inc will contain our filter handler).

A basic views_more_filters.module file:

<?php
/**
 * Implements of hook_views_api().
 */
function views_more_filters_views_api() {
  return array('api' => 3);
}

Then define your filter in views_more_filters.views.inc:

<?php
/**
 * Implements of hook_views_data().
 */
function views_more_filters_views_data() {
  return array(
    'node' => array(
      'author_select' => array(
        'group' => t('Content'),
        'title' => t('Author UID (select list)'),
        'help' => t('Filter by author, choosing from dropdown list.'),
        'filter' => array('handler' => 'views_more_filters_handler_filter_author_select'),
        'real field' => 'uid',
      )
    )
  );
}

Note that we set author_select as a machine name of the filter, defined filter handler ('handler' => 'views_more_filters_handler_filter_author_select') and a field we will filter by ('real field' => 'uid').

Now we need to implement our filter handler. As our filter functions just like default views_handler_filter_in_operator, we simply extend its class in views_more_filters_handler_filter_author_select.inc file:

<?php
/**
 * My custom filter handler
 */
class views_more_filters_handler_filter_author_select extends views_handler_filter_in_operator {

  /**
   * Override parent get_value_options() function.
   *
   * @return
   *   Return the stored values in $this->value_options if someone expects it.
   */
  function get_value_options() {
    $users_list = entity_load('user');

    foreach ($users_list as $user) {
      $users[$user->uid] = $user->name;
    }

    // We don't need Guest user here, so remove it.
    unset($users[0]);

    // Sort by username.
    natsort($users);

    $this->value_options = $users;

    return $users;
  }
}

We haven't had to do much here: just populate options array with a list of our users, the rest is handled by parent class.

For further info see:

Community
  • 1
  • 1
Shevchuk
  • 515
  • 5
  • 8
  • This is the first solution I've found to this problem which is clean and makes sense. :) – Patrick R. Nov 16 '14 at 16:33
  • Great solution, just one question here though. Should this change all the User filters (including the existing ones also) to dropdown? – Pratip Ghosh Sep 01 '15 at 12:50
  • Great answer. It is probably best to use `hook_views_data_alter` , since you are modifying an existing array. – argiepiano Jun 28 '17 at 16:50
  • If somebody wants to set default selection to current user it's as simple as adding few lines before `return $users;` Here you go: `global $user; if (array_key_exists($user->uid, $users)) { $this->value = $user->uid; }` – NullIsNot0 Apr 06 '18 at 16:11
17

Yes, this is possible. Its not particularly tough to do this... but its slightly tedious. You need to create two views

  1. The first view is a list of users on your system (a View of type Users). This user list is displayed as a dropdown instead of a list (using jump menu view style). Clicking on any user within this dropdown will call the second view with the uid (user id) of the selected user as the argument in the URL. This view is a block.
  2. The second view is a simple Node listing. It is a page view at a particular URL. It takes 1 argument which is the uid (user id) of the user.

Detailed Steps

  1. Download the Ctools module http://drupal.org/project/ctools Enable the Chaos Tools Module. This module provides a Views Style Plugin called "Jump Menu"
  2. Create a new view of type Users and NOT type Node which you usually create. In the fields add User: Name and User: uid. For the settings of User: uid, make sure you click on Rewrite the output of the field. The rewritten output of the field should be my_node_list/[uid]. Make sure you select the exclude from display checkbox.
  3. In the settings for Style in the view, select the Jump Menu style. Click on the settings for the style. Make sure the Path dropdown has User: uid choosen
  4. Add a block display to the view. Name the block User Drop Down
  5. Save the view
  6. Add the block User Drop Down to any region in your theme e.g. Content Top (usually the best) or left sidebar. Make sure the block is only visible at the urls my_node_list/* and my_node_list by setting the block visibility settings
  7. Now create another view of type Node. Add an argument field User: uid. Add the fields you are interested in e.g. Node: title, User: Name etc.
  8. Add a page display. Let the page be at the url my_node_list
  9. Save the view. Test the dropdown with its list of users on the system at http://yoursitename/my_node_list
Sid Kshatriya
  • 4,733
  • 2
  • 21
  • 29
  • @Linda: did the answer help you out? – Sid Kshatriya Oct 11 '10 at 10:31
  • I think this is irrelevant to the OP question. Jump menu is different thing from views filter. The first jumps to a page, the second filters the results. – swan Feb 20 '12 at 21:04
  • @swan - you're right that a Ctools Jump menu is a different thing than a filter, but because there is no out-of-the-box way to do a drop-down Views filter on user name, this is a clever workaround. – Joshua Stewardson Mar 19 '13 at 21:33
-2

http://drupal.org/project/better_exposed_filters Check this one

Igor Rodinov
  • 471
  • 3
  • 5
-3

Found a simple solution here. http://bryanbraun.com/2013/08/06/drupal-tutorials-exposed-filters-with-views

user2658838
  • 80
  • 1
  • 2
  • 9
-3

I think you just have to choose "Taxonomy:term The taxonomy term ID" instead of "name".

Mike Pennington
  • 38,579
  • 16
  • 126
  • 167
  • Taxonomy isn't the same thing as User - so that filter wouldn't do the trick for this question. That's a good answer for a taxonomy filter though! – Joshua Stewardson Mar 19 '13 at 21:32