1

I am working with Services module to create an API. I have created a View that shows list of Organic groups.

Now I need to add a field which will show if the current user is member of the group or not. Can anyone please tell me how to do this?

2
  • Are you using Services Views to retrieve your view from your REST API?
    – Shawn Conn
    Commented Jun 23, 2016 at 3:22
  • @ShawnConn Yes, I am using Services Views
    – Malik
    Commented Jun 23, 2016 at 7:58

1 Answer 1

1

There is no field or direct method to show "If the current user is member of the group or not". In your case I have found a solution here:

  1. Create a view - showing list of all groups(you have already created).
  2. Add new Relationship - OG membership: OG membership from Node group
  3. Add new field - (OG membership from node group) OG membership: Group ID (Group ID), you can exclude it from display.
  4. Add new field - Global: PHP (you have to enable Views PHP module for this field) and put below code in this field:

    <?php
    
    global $user;
    $account = user_load($user->uid);
    $ugrpid = $account->og_user_node['und'][0]['target_id'];
    
    if($row->gid == $ugrpid){
      echo "Already Subscribed.";
    }else{
      echo "Not member of this Group.";
    }
    
    ?>
    

This is how my view look like( Screenshot ).

enter image description here

3
  • Its not working. I followed your instructions. When I look at the auto preview under the view it always says "Not member of this group". And when I get URL using RESTclient, there is no output. Here is screenshot of View : i66.tinypic.com/2dak27q.jpg Here is screenshot of RESTclient : i65.tinypic.com/2cpccgl.png
    – Malik
    Commented Jun 23, 2016 at 8:31
  • Please check the Screenshot added. Commented Jun 23, 2016 at 9:17
  • In the relationship, where you have "OG membership from Node group", I don't have this option.. I got this option "OG membership from Content group" ? Please suggest what I should do ?
    – Malik
    Commented Jun 23, 2016 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.