For User Reference, I'm trying to create a view that will only show members of an Organic Group when creating group content, but the view I create returns no results. I'm using the relationship "Entity Reference: Referenced Entity" in my view which is A bridge to the Content entity that is referenced via og_group_ref.

In my URL I call out the group ID (e.g., /node/add/task?og_group_ref=453).

Any ideas on what I'm doing wrong?

Thank you in advance!

Comments

loopduplicate’s picture

Project:References» Entity reference
Version:7.x-2.0» 7.x-1.x-dev

This support request should be filed under the Entity Reference module so I switched it to that project. With that said, I'd like to be able to do the same thing :)

klucid’s picture

Thanks for switching it. Hopefully we get a solution soon!

loopduplicate’s picture

Well, not sure if you can do it with Entity Reference Prepopulate or not.

I ended up using hook_form_alter on a couple of node edit forms to populate all user entity reference fields with members from the project, which is the name of my group type, referenced in the URL or from the project that was previously saved in the node. Then, under manage fields, I edited each user entity reference field and chose to redirect users if the project was not specified in the URL. Next, in the OG permissions for the project group, I disabled editing of the project field by members so that the only way they can choose a project when creating content is to specify it in the URL.

Below is my hook_form_alter code.

<?php
/**
 * Implements hook_form_alter();
 * Depends on the OG Extras module to get a member list from a group's nid
 * Prepopulates the user reference fields on the todo and bug edit form based on
 *  the project that is selected.
 */
function tmi_misc_form_alter(&$form, &$form_state, $form_id) {
  switch (
$form_id) {
    case
'todo_node_form':
    case
'bug_node_form':
     
$bundle_name = $form['type']['#value'];
     
$fields_info = field_info_instances('node', $bundle_name);
     
// get node id of project if a project is selected
     
$gid = ($form['og_group_ref']['und']['#default_value'] ? $form['og_group_ref']['und']['#default_value']['0'] : false);
     
// repopulate user reference fields with users from selected project
     
if ($gid) {
       
// get member ids from the project node id
       
$member_ids = og_extras_member_ids($gid, 1000);
       
// store array of values to populate user reference fields
       
$members = array(
         
'' => '-None-',
        );
        foreach (
$member_ids as $uid) {
         
$member = user_load($uid);
         
$members[$uid] = $member->name;
        }
       
// sort the member list by name
       
asort($members);
        foreach (
$fields_info as $field_name => $value) {
         
$field_info = field_info_field($field_name);
          if (
$field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'user') {
           
// empty the default list of users
           
$form[$field_name]['und']['#options'] = array();
           
// populate the user reference fields with members from the project
           
foreach ($members as $uid => $name) {
             
$form[$field_name]['und']['#options'][$uid] = $name;
            }
          }
        }
      } else {
// if no group is selected
        // Give informative message if admin tries to create a node without using entity
        //  reference prepopulate.
       
global $user;
        if (
in_array('administrator', array_values($user->roles))) {
         
drupal_set_message('User reference fields have been hidden
          because a project was not referenced in the URL or was not previously saved.'
, 'status', false);
        }
       
// Unset user reference fields
       
foreach ($fields_info as $field_name => $value) {
         
$field_info = field_info_field($field_name);
          if (
$field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'user') {
            unset(
$form[$field_name]);
          }
        }
      }
      break;

    default:
      break;
  }
}
?>

@Damien and Amitaibu: Is there an easier way?

loopduplicate’s picture

Well, I needed to make a change. I now allow members to choose a group. If an admin tried to create content without prepopulating the group, they are shown a message but the user reference fields are no longer hidden.

New hook_form_alter code below:

<?php
/**
 * Implements hook_form_alter();
 * Depends on the OG Extras module to get a member list from a group's nid
 * Prepopulates the user reference fields on the todo and bug edit form based on
 *  the project that is selected.
 */
function tmi_misc_form_alter(&$form, &$form_state, $form_id) {
  switch (
$form_id) {
    case
'todo_node_form':
    case
'bug_node_form':
     
$bundle_name = $form['type']['#value'];
     
$fields_info = field_info_instances('node', $bundle_name);
     
// get node id of project if a project is selected
     
$gid = (isset($form['og_group_ref']['und']['#default_value']['0']) ? $form['og_group_ref']['und']['#default_value']['0'] : false);
     
// repopulate user reference fields with users from selected project
     
if ($gid) {
       
// get member ids from the project node id
       
$member_ids = og_extras_member_ids($gid, 1000);
       
// store array of values to populate user reference fields
       
$members = array(
         
'' => '-None-',
        );
        foreach (
$member_ids as $uid) {
         
$member = user_load($uid);
         
$members[$uid] = $member->name;
        }
       
// sort the member list by name
       
asort($members);
        foreach (
$fields_info as $field_name => $value) {
         
$field_info = field_info_field($field_name);
          if (
$field_info['type'] == 'entityreference' && $field_info['settings']['target_type'] == 'user') {
           
// empty the default list of users
           
$form[$field_name]['und']['#options'] = array();
           
// populate the user reference fields with members from the project
           
foreach ($members as $uid => $name) {
             
$form[$field_name]['und']['#options'][$uid] = $name;
            }
          }
        }
      } else {
// if no group is selected
        // Give informative message if admin tries to create a node without using entity
        //  reference prepopulate.
       
global $user;
        if (
in_array('administrator', array_values($user->roles))) {
         
drupal_set_message('A project was not referenced in the URL or was not previously saved.
            User reference fields have been populated by all users that are in groups you belong to.'
, 'status', false);
        }
      }
      break;
 
    default:
      break;
  }
}
?>
klucid’s picture

It's been a few months and I'm trying to get this working still. I noticed there is now an option to filter users by a "references" view. I can do that, but I'm unsure of how to get the entity reference prepopulate group ID from the URL to pass into views as a contextual filter. Any help would be much appreciated!

Example: when at /node/add/task?og_group_ref=453 I would like to filter my "reference" view to group members of group 453 only.

presleyd’s picture

There is a patch here #1699378: Dynamic Views Arguments via Tokens to add Token support to the arguments an entity reference field passes to the filtering view. With this patch you could pass a token for the URL to the a view with an entity reference display and have the view do all the filtering.

Yuri’s picture

@presleyd or someone else: did someone accomplish this issue by using the patch mentioned in #6? I used as token [node:og_group_ref] and nothing shows up in the ER field.
If someone has another solution, please let me know
Thanks