Sorry, I'm sure this is an easy one, but I just can't seem to figure out what kind of argument to use to restrict access of a view (to users with a relationship of any type) on users' profile pages. What kind of php code snippet could I use and would I apply it within the block or view settings? Any help would be much appreciated!

Comments

alex.k’s picture

Assigned: Unassigned » alex.k

Hi, it's as simple as this:

global $user;
//show only on user/* URLs
if (arg(0) != 'user' || !is_numeric(arg(1))) { return FALSE; }
$profile_uid = arg(1);
return user_relationships_load(array('between' => array($user->uid, $profile_uid), 'approved' => 1), array('count' => TRUE));
alexevasion’s picture

Alex,

Thanks so much for the quick response. I don't mean to be a continual bother, but I still can't seem to get this to work. I'm attempting to implement this snippet in an User Relationship: RTID argument for a block view. My default argument type is User ID from URL and the action to take if the argument is not present is to Display all Values. I don't think the rest of the validator stuff seems to apply to my case, but it's also above my level of understanding. Do you know what I'm doing wrong?

alex.k’s picture

Oh, I misunderstood what you were trying to do... If you have a view that just needs to be shown to users who have a relationship, it's not necessary to do anything in the view. That snippet was for the block visibility option in your block config. It sounds like you do not need anything in argument settings, as it is more of an access control restriction than a data filter. Let me know if that isn't the case :)

alexevasion’s picture

I wish it worked in the block, but I already employ another snippet to show a block listing a user's nodes on their profile:

if (arg(0) == 'user' && is_numeric(arg(1)) && is_null(arg(2))) {
  return TRUE;
}
else {
  return FALSE;
}

I'm not sure if this interferes or whether I am going about this all wrong. Basically, I only want a user's friends (relations) to be able to view the block. Is there an easy recipe for this? Thanks again!

alexevasion’s picture

Any other thoughts as to what I might be doing wrong?

alex.k’s picture

@alexevasion sorry this dropped off the radar. Is it still an issue? It seems that the snippet in #1 does everything including what's in #4, with a small correction:

global $user;
//show only on user/* URLs
if (arg(0) != 'user' || !is_numeric(arg(1))) { return FALSE; }
if (!is_null(arg(2)) { return FALSE; }
$profile_uid = arg(1);
return user_relationships_load(array('between' => array($user->uid, $profile_uid), 'approved' => 1), array('count' => TRUE));
rjbrown99’s picture

Alex,

I'm now trying to do the same thing, and I agree this would work well for a block. How about implementing this for a view with a page display? What I'm looking for is something under Basic Settings -> Access in views that would allow me to restrict the view output to only people with a relationship of type 1. I'm definitely going for access control and not arguments of what content to show. I want to restrict the view output to only specific users with an established relationship.

Do you have a thought for this use case? Thank you.

alex.k’s picture

Hmm, perhaps try adding the Global: Null argument and setting its validator to PHP code. Then the snippet acts as an access control check.

rjbrown99’s picture

Thanks very much Alex - I'll test that out and report back with findings. Assuming it works I'll post more details in case others have that same need.

rburgundy’s picture

Please do come back and post your findings. Any additional information will be very helpful to myself and others that are learning =)

I'm trying to get this to work on a content profile page that a user creates instead of the user/uid page.

rjbrown99’s picture

OK, my findings are that it worked well :)

My goal was to have a view that was only displayed to users that have a current relationship of "friend". In the event that the relationship did not exist, I want to show the user an intelligent message that encourages them to become friends.

To accomplish this, I took the view that I already had and created an argument. The argument is "Global: Null", Provide Default Argument, User ID from logged in User, and then a validator of PHP code. The PHP code is:

global $user;
$profile_uid = arg(1);
return user_relationships_load(array('between' => array($user->uid, $profile_uid), 'approved' => 1), array('count' => TRUE));

In my case, since this view requires a user argument to even be displayed I did not need the other checks. The action to take if the argument does not validate is "Display empty text".

Now I have two instances where the empty text is displayed - when the view output is actually empty or when the users are not friends. The next part was to handle this appropriately to return the correct message to the user. To accomplish this, I went over to empty text, changed the input format to PHP code, and then I inserted an if/else statement to handle that condition.

Thanks Alex K!

alex.k’s picture

Status: Active » Fixed

Awesome, thanks for posting back! If only this could be captured in some documentation page...

rburgundy’s picture

Thank you very much rjbrown99!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

rjbrown99’s picture

Quick update.

I found it annoying that my admin account was always locked out of the view results because the admin has no relationship with the user. I changed my code to this:

global $user;
$profile_uid = arg(1);

if ($user->uid == 1) {
  return TRUE;
} else {
  return user_relationships_load(array('between' => array($user->uid, $profile_uid), 'approved' => 1), array('count' => TRUE));
}

to solve that problem. This code snippet now restricts access to the view by relationship EXCEPT for the admin account. The admin can always see the view output.

coolhandlukek2’s picture

subscribe!

Sorry to be a bit dim, I have added the code to the RTID argument and set the validator to basic, but it doesnt seem to work.

Am i doing something wrong?

Thx

coolhandlukek2’s picture

Hi, scrub that! Didnt read the post properly thx

Crom’s picture

Thank you. Works perfectly!

Anonymous’s picture

Is there a Drupal 7 solution?

hockey2112’s picture

Issue summary: View changes

I, too, am in need of a Drupal 7 solution. Can anyone please provide guidance on how to accomplish that?

------EDIT------
I ended up printing my views content in a Panel page and controlling access via user relationships using the options in Panels.