0

Im using drupal 7 and group module

I am trying to run some code only if the current page is a group.

here is my code

<?php
$arg = arg(0);
$arg1 = arg(1);
global $user;
if($arg ='group' && is_numeric($arg1)) {
    $group = group_load($arg1);
    if($group->getMember($user->uid)) { 
       print ('user is member');  
    };
};
?>

If this is placed on a group page it will print user is member if the current user is a member. If the current user isn't a member it will not show anything. Which is what it is supposed to do.

If I navigate to a none group page I get an error 'Error: Call to a member function getMember()', but the if statement should prevent this?

How do I check if the current page is a group entity?

Thanks

Edit

I wanted to return true if a group page and user is a member of the group. Using menu_get_object by Jaypan I used the code below to show. Im checking if page is group if so loading the group then checking if the user is a member of the group.

if (!empty(menu_get_object('group')))
   { 
   $group = group_load(arg(1));
   if($group->getMember($user->uid)) { 
       return TRUE;  
    }
}
0

You can get the object for the current page in Drupal 7 using menu_get_object().

if ($group == menu_get_object('group')) {
  // do something with $group
}
| improve this answer | |
  • Hi, Thanks for replying but this doesn't work and I don't know the syntax to make it work. I think it isn't working because $group has already been loaded as a group, what I want to do is check that the path is a group before loading $group. So I need to get type from menu_get_object and check if it is of type group, if menu_get_object([0]) = 'group', or menu_get_object($type) = 'group'. But I do't know how to structure menu_get_object. – Imogen Johnson May 3 at 10:25
  • I managed to do it by checking if menu_get_object('group') is set first. – Imogen Johnson May 3 at 11:09
  • I'm not sure why you think this doesn't work. It only loads $group when menu_get_object('group') returns a group. If you are on a group page, the if() conditional is entered. If you are not on a group page, the if() conditional is not entered. And when on a group page, this will load the group for you, so you don't need to call node_load() after the fact, as menu_get_object('group') will have already returned the group on group pages. – Jaypan May 3 at 15:13
  • I put <?php if ($group == menu_get_object('group')) { print ('true'); } ?>, nothing happened, I checked log it said group not defined, this was on a group page. – Imogen Johnson May 3 at 20:19
  • In Drupal, printing often doesn't print to the screen. Try drupal_set_message('Here');. – Jaypan May 3 at 20:27
0

checking if page is group if so loading the group then checking if the user is a member of the group.

if (!empty(menu_get_object('group')))
   { 
   $group = group_load(arg(1));
   if($group->getMember($user->uid)) { 
       return TRUE;  
    }
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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