1

I am trying to display the ID of the group the user is currently viewing. I created a block, and added the following PHP code into it:

<?php
$group = og_context();
if ( !empty($group) ) {
   print "$group->nid";
}
?>

Problem is, when I run it, I get the following error:

Notice: Trying to get property of non-object

Any idea why this is happening, and how to fix it? Seems like it should be easy enough of a task.

8
  • <?php print arg(1); ?> if it is a node page...
    – Anil Sagar
    Mar 17, 2014 at 8:53
  • Which version of Drupal are you using ?
    – Anil Sagar
    Mar 17, 2014 at 8:55
  • I am using Drupal 7.26 Mar 17, 2014 at 8:55
  • @AnilSagar -- your suggestion worked. Can you please explain how this works? I am not super familiar with PHP. Thanks! Mar 17, 2014 at 8:57
  • Each content created using content types in Drupal is a node which will have nid and path node/nid . Groups also nodes.... you can read path arguments using arg function.. so arg(0) is "node" and arg(1) is nid of node... Find more here api.drupal.org/api/drupal/includes%21bootstrap.inc/function/arg/…
    – Anil Sagar
    Mar 17, 2014 at 9:00

2 Answers 2

0

In Drupal 7 the NID is the group ID so you could just output $node->nid.

2
  • But I need the group ID to be printed on all nodes associated with the group, not just on the group page itself. Apologies if I wasn't clear in my original ask. Mar 17, 2014 at 16:39
  • Is it possible for a content entity to be a member of more than one group on your site?
    – Rob
    Mar 17, 2014 at 18:23
0

The basic problem is that og_context() doesn't return an object, or the group node - it returns an array of group information.

Basically, using your example code, you want $group['gid'], assuming that all of your organic groups are based off of nodes. If they aren't, you'll want to also look at $group['group_type']. See http://drupalcontrib.org/api/drupal/contributions!og!og_context!og_context.module/function/og_context/7 for more information about this function.

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.