How can we pass the group arguments in Views? It's super easy with 6.x but I can't find it with 7.x.

CommentFileSizeAuthor
#24 Picture 94.png51.43 KBpetednz
#23 view.png13.88 KBmedden
#18 og_viewscontext.zip2.54 KBtorrance123
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Swebmas’s picture

Version: 7.x-1.0-alpha1 » 7.x-1.0
Swebmas’s picture

Assigned: Swebmas » Unassigned
geerlingguy’s picture

Version: 7.x-1.0 » 7.x-1.x-dev

I second this request.

amitaibu’s picture

Status: Active » Fixed

In D6 the group Id == node ID. But this is not the case in D7. So either you do it manually via Views API, or you use a module such as OG-context to pass that group for you.

In brief:

$nid = 1; // For example you need to get the group ID of node ID 1.
$group = og_get_group('node', $nid);
$gid = $group->gid; // <-- This is the group ID you need to pass.

Status: Fixed » Closed (fixed)

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

geerlingguy’s picture

Status: Closed (fixed) » Active

How could I do the opposite of what you did in #4? For example, I have a group ID, and I want to get the nid of the group's node... is there a way to do that without making a DB query to find it myself?

For example, right now, to get a view to show up on a group page, but displaying some content in the group's node, I have the following my argument validation, to get the nid from the gid:

  // Get the node id for that group's group ID.
  $group_nid = db_query("SELECT etid FROM {og} WHERE gid = :gid", array(':gid' => $first_group_id))->fetchField();

(For my reference: http://archstldev.com/node/793).

amitaibu’s picture

$group - og_load($gid);
if ($group->entity_type == 'node') {
  $nid = $group->etid;
}
amitaibu’s picture

$group = og_load($gid);

amitaibu’s picture

Status: Active » Fixed
geerlingguy’s picture

Woah. Cool - didn't know about og_load(). Very nice!

Sol Roth’s picture

I have a view with the argument Fields: group_audience - gid (it's displaying a contest_entry content type which allows members of the group to vote on each node.)

In the group node, I have a field in the group that is a select list containing the stage of the contest (open for voting etc). How do I filter the view to only show up when the contest is in the voting stage (the contest stage field is selected to "voting" on the group node)? I'm dealing with a bug with minipanels (http://drupal.org/node/1049154), so I'm hoping to accomplish this entirely with views.

Would using a custom php validator be a good way to accomplish this?

Sol Roth’s picture

Just incase anyone needs this or had some input / a better way. I accomplished what I needed to do by using the argument

Fields: group_audience-gid with "provide default argument" and

Default arguement type
"Current group from context"

In Validator I choose "PHP Code"

$group = og_load($argument);
$node = node_load(($group->etid));

if ($node->field_contest_stage['und'][0]['value']=='Voting'){return TRUE;}

neopoet’s picture

Apologies for hijacking this thread, but my question is similar --

How do you get a group ID if you only have a node that has been associated with that group (the node has been "submitted into" the group, and is not the group node itself)?

Thank you very much.

gilzero’s picture

Subscribe

Status: Fixed » Closed (fixed)

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

torrance123’s picture

Status: Closed (fixed) » Active

I'm reopening this ticket as php code inside a views argument is not the correct solution.

USE CASE: I want to add a tab to my group node that pulls in a list of fields from the nodes that are associated with my group. Normally, I would set the view path to 'node/%/view', and set the argument appropriately. But since nid != gid, I cannot filter out nodes that are not audience to this node.

torrance123’s picture

If it helps others, we solved this by using the context filter "Fields: group_audience - gid" and then additionally creating a tiny module which uses hook_views_pre_view() to translate the nid to the group ID, if it exists.

We had three views that needed this translation. This was the little bit of module code we used (module called 'vapi'):

function vapi_views_api() {
  return array(
    'api' => 3,
  );
}

function vapi_views_pre_view(&$view, &$display_id, &$args) {
  if ($view->name == 'events' && $view->current_display == 'page_2' ||
      $view->name == 'group_files' && $view->current_display == 'page' ||
      $view->name = 'discussions' && $view->current_display == 'page_2') {
    $group = og_get_group('node', $args[0]);
    $args[0] = $group->gid;
  }
}

Ideally, this would be an generic contextual filter rather than running as an adhoc hook_views_pre_view(), but I don't know how to do that as yet.

torrance123’s picture

FileSize
2.54 KB

Yay, I managed to write a contextual filter (albeit with the field name hardcoded [field_data_group_audience]). If, for example, you are at /node/%/viewspage, and you want to load all nodes that are members of the group represented by the wildcard, this will do the trick.

<?php
function og_viewscontext_views_data() {
  $data = array();
  
  $data['field_data_group_audience']['table']['group'] = t('Group');
  
  $data['field_data_group_audience']['gid'] = array(
    'title' => t('Group Node ID'),
    'help' => t('Translate the node ID to the corresponding group ID.'),
    'real field' => 'group_audience_gid',
    'argument' => array(
      'handler' => 'og_viewscontext_handler_argument_numeric',
    ),
  );
  
  return $data;
}

class og_viewscontext_handler_argument_numeric extends views_handler_argument_numeric {
  function construct() {
    parent::construct();
  }
  
  function set_argument($arg) {
    if (is_numeric($arg)) {
      $group = og_get_group('node', $arg);
      $arg = $group->gid;
    }
    $this->argument = $arg;
    return $this->validate_arg($arg);
  }
}
?>
greta_drupal’s picture

@torrance123 : I have the same use case. Did you find a solution? Might it work with D6.22? Would you mind expounding on your "normally, I would..." approach? The the path settings for my views node tabs are tripping me up.

medden’s picture

I found a very simple way to pass current OG as an argument in views.

It works for both adding new content to a group.
e.g. url
node/add/group_post?gids_node[]=1&destination=node/1
and also for editing existing group content.
e.g. url
node/16/edit

Inside any view, add a relationship for (Content) Group membership: Node group membership

Then add a contextual filter/argument for (group membership) OG membership: Group gid

Under the settings select Provide default value

Type PHP contextual filter code
Code:

return ($_SESSION['og_context']);

I got this to work for setting organic group permissions for the new view tab on the media module library browser (media-2-x). Now each group has it's own library of files!

enricpera’s picture

Thanks medden,
It works for us!!!!

petednz’s picture

Could either medden or creatcat flesh out the steps so I can see why i am missing any such Relationship.
View is of type 'content' using Views3 on D7
WHen I look under Relationships I do not see any with word 'membership'
Ditto now options under Contextual Filters with the word 'membership'

So maybe I am missing something way back at the Group Field Settings? WHat I have is
Group - Node entity
- Group type
- Group visibility
and
Post - Node entity
- Groups audience
- Group content visibility

Thanks for any pointers/screenshots you can offer

medden’s picture

FileSize
13.88 KB

Hi petednz,

Are you sure you are looking in 'relationships' and not in 'contextual filters'?

I know OG module has renamed some of it's terms, but I'm using OG-7.x-1.3 and also have most of the modules, like 'access control' enabled. Perhaps it's a sub module that gives the membership relationships.

I've attached an image of my add relationship form so you can see.

petednz’s picture

FileSize
51.43 KB

always happy to have fundamental questions asked - but yes in this case i am not confused - here is what i see

chintan4u’s picture

hello torrance123,

Your module saved me from disaster.
Thanks a lot.

chintan4u’s picture

Issue summary: View changes

undoing comments. not sure how the heck they got added to original post