How to use current user as related field in Drupal 7 Views? - drupal-7

When users register they associate to one or more locations via user profile. Location is a custom content type. I have a custom field in the user profile defining a relation to this location.
I have a user-type view that returns un-authenticated users and is restricted to elevated role users. I'd like to add a filter that only returns un-authenticated users whose associated location value(s) match one or more location values from the current user requesting the view.
I can filter by the associated location, but I can't relate that value to the current user's value(s). I feel like I might be able to do this if I could add the current user uid to the relationships area and not just the contextual filters area. Can I do this without resorting to a custom filter or relationship?
EDIT:
There may be some confusion so maybe this will help clarify.
User -> Profile -> Location is the data association chain. I want to only show users whose Location value(s) match those of the currently logged-in user.
User (any) -> Profile (Student) -> Location(s) == User (current) -> Profile (admin) -> Location(s)
I could write SQL to do this but it's not clear if this is possible to do using views alone, or if I need a custom relationship or filter.

The only way to do this within views is the following:
Make View of using User type
add a Relationship to a Profile2 type (in my case 'Student')
Add Contextual Filter of type profile:student:location
select "provide default value"
type: "php code"
Use the following code:
global $user;
$profile = profile2_load_by_user($user->uid,'facilitator');
if (!$profile) { return ''; }
if (!isset($profile->field_location['und'])) { return ''; }
$locations='';
foreach ($profile->field_location['und'] as $location) {
$locations.=','.$location['nid'];
}
return substr($locations,1);
Under "More", Set "Allow Multiple Values" to TRUE
Save your Contextual Filter
Using PHP in this manner, you can load the current User and get associated values to filter by. This may be useful in many other contexts and circumstances.

Related

Drupal7 - Filter view based on current user's roles?

Using Drupal7, Views3 and the Node Reference module, I'm using a view to populate a node reference field on a custom content type. (So it's a view of "Reference" display type.)
I want to filter this view to show (and allow users to select) only:
published content (OK)
content of a certain type (OK)
only nodes created by the current user (OK)
OR if the current user is admin or some other role, bypass the previous filter (3) and show all nodes (but still respect filters 1 and 2) (not OK)
Filters 1 and 2 are always mandatory; then either filter 3 OR 4 must also be mandatory. I know I can rearrange filters into filter groups to make this work. The problem is that I cannot find a way to build filter 4.
For filter 3, I had to bring in a Relationship of type "Content: Author", which made a lot of new filters appear, including the "User: Current" that I used for filter 3 (node author == current user).
For filter 4, I need to filter based on the role of the current user (not author), and I cannot find how to do that.
In the filters list there's a new "User: Roles" filter available, but it only refers to the "Content: Author" relationship, so it only checks the node author's roles. That's not what I need.
I'm guessing I have to add a new Relationship to bring in the current user data (something like "User: current"), and then filter on that data, but I cannot find that in the Add Relationship screen.
Any idea how to do that?
Thanks in advance.
Update:
Note: when editing the node reference field (in my custom content type), I see there's a "views arguments" field that allows to pass arguments to the view. It's not clear whether this field accepts tokens, but if so, maybe I could use that field to pass the current user ID to the View. But then I don't know how to use that in Views...
After posting the same question on drupal.stackexchange, I was suggested a solution using some PHP code in Views (contextual filter). It's the best solution I've found yet, so unless someone can propose a better solution, I'll stick with this one.
Here is the solution:
https://drupal.stackexchange.com/questions/38205/alter-field-settings-using-hook/38922#38922
And here is the gist of it:
In the View, add a contextual filter on Author: Uid, and use the following settings:
WHEN THE FILTER VALUE IS NOT AVAILABLE:
Provide default value
Type: PHP Code
Use the following code:
global $user;
if (in_array('administrator', $user->roles))
{return -1;}
else
{return $user->uid;}
WHEN THE FILTER VALUE IS AVAILABLE OR A DEFAULT IS PROVIDED
Specify validation criteria
Validator: PHP Code
Use the following code:
if ($argument == -1)
{return FALSE;}
else
{return TRUE;}
Action to take if filter value does not validate: Display all results for the specified field (this is what will give admins access to all results)
And that's it!
Notes:
Compared to my initial settings, the relationship (Content: Author) isn't needed anymore; neither is the "Author" filter (which was brought in by the relationship anyway).
For Drupal 6, the condition in the first PHP snippet should rather be if (in_array('super user', array_values($user->roles)))
You can allow other roles as well, simply by editing the condition above. For instance:
if (
in_array('administrator', $user->roles) ||
in_array('editor', $user->roles)
)
After adding the author relationship, there should be a new filter criteria User: Roles.
Inside your views filter criteria, you will need to have 2 filter groups combined by OR; First group contain Filters 1, 2 and 3. And the other group should contain filters 1, 2 and 4.

In Drupal Site:how to display the list of corresponding users by using user reference module

I have 3 user role that
Senior->users(senior1,senior2)
Sub_Senior->users(subsenior1,subsenior2)
junior->users(j1,j2,j3,j4)
the user subsenior1 is belonging to senior2 , user subsenior2 is belonging to senior1.
the users j1,j2 belonging to subsenior1 and users j3,j4 belonging to subsenior2 referenced by using user reference fields
but i am not able to list the corresponding users in views page like when i click the senior1 user it will list the corresponding subsenior2 user(corresponding all referenced users).
or
modules or technique is possible to create the user hierarchy and list corresponding users in views page.
I'm extremely new to drupal any one help me ..
thanks in advance..
What you have to do is to create a view that lists users and which takes a user id as an argument. When no user id is given it should list all users. When a user id is given the view should list all users who are referenced to the user whose id is given as the parameter.
In the views output against each user there should be a link that links to the same view with the user id of the user in the row passed as a parameter. Now clicking on that link the view will be loaded and show the child users under that user.
or
Use the Organic Group module for user hierarchy.

Drupal 7 views contextual filter restrict content based on current users role

I'd like to restrict access to a view based on the CURRENT users role. Not the author. For example, if a user has the authorized user role then they can see the content of the view. If a user is anonymous then they are shown the No Results Behavior of the view. I can't believe there is no way to do this. I know there is the Access settings, but I don't want the anonymous user given an access denied message.
One method I can think of:
Use hook_views_query_alter(). Check if the current user belongs to the set of roles you are interested in. If he doesn't, add a condition which is always false, such as 0 = 1. To see how to add such a condition in code, see the example on this page: http://api.drupal.org/api/views/views.api.php/function/hook_views_query_alter/7. The resulting view will not have any result on adding this condition.
Neerav Mehta.
Drupal Development

upload files to specific user

I want to let admin upload files (PDF files) to a separate user (client), such as bill, information and documentation.
I think to Add new content type with some fields, like file field, but I don't know how to add users list, so admin can choose file to upload and corresponding user.
After I finish this step, how can I display these files to user when login?
To reference the user, you could install the entity reference module.
http://drupal.org/project/entityreference
You can use that to add a field that will reference users.
As far as listing files for the user, you can use Views to make a list of your new content type, and filter it by the user who is logged in.
To elaborate more, Install the Views module, and make a new View, with a content-type filter for your new type, and a contextual filter for current User, and then add a Field for the file field, and configure the field however you would like the file to be displayed to the user.
Depending on what you want, you could also set that view as the frontpage of your site, so that people will see it when they log in.
To filter by current user, when you add the Contextual Filter, in the "WHEN THE FILTER VALUE IS NOT IN THE URL" section, select "Provide Default Value" and then select the type "User ID from logged in user"

Best way to populate a dropdown sitewide that is

I am using CakePHP 1.3 and a layout which includes a dropdown of organizations a user has access to administer, so I'm trying to populate that dropdown with organizations that contain the userid that is logged in, but I want to populate it before the user sees anything so they can use it in the header. The dropdown needs to appear on every page once logged in. I tried adding the query to pull these organizations in the appcontroller, but userid was not yet available to use in before filter. Where or how should I do this? Should it be in session or is there a better construct to use? Element?
In my app it's no problem to use the user_id from within the beforeRender (if you are using the Auth-Component).
You can use it with $this->Auth->user('id').
I would do it like this: Check in the AppController if the user is logged in. If he is, pull your wanted information from the database (or whereever you get your information from) and store it in a variable called $dropdown for example.
If the user is not logged in, $dropdown will be false.
You now make this variable available to the view with $this->set(compact('dropdown'))
Now in your layout (this is important to you have it on every page) you can easily do a check if $dropdown is false or not. If not, you can work with your variable and show the user your wanted dropdown.

Resources