Hi,
Entity Reference is great and it even became greater now it is possible to dynamic generate select lists with a view.
A while ago it was only possible with the old References module and only for user or node references.

My problem now is very strange. I create a view that gives perfect results (an array of several items). When I select this view as list for my entity reference field I always only get to see the last item of the view result.
This strange behaviour only appears when I use a contextual filter in my view AND when I create a view to select users.
So when I do the same to select nodes, it works perfect!

Does anybody know why it is not possible to use a view to populate a select list for an entity reference field to a user in combination with a contextual filter?
(I'm using Drupal 7.14, views 7-3.3 and Entity Reference 7-1.x-dev from June 18th 2012)

Comments

maxplus’s picture

Hi,

I'm starting to think that the problem is not related to the entity reference module but rather to the Field Collection module.
I did all kinds of tests:
- using contextual filters and not using contextual filters
- using relations and not using relations
- using target entity's; users, field collections, nodes

=> when I still keep using contextual filters and relations in views but my target entity is a Node: it works.
=> even when my source is a entity reference field inside a field collection

maxplus’s picture

Hi,

my solution was finally very simple: just use the contextual filter right away to filter on the reference field instead of first using a relation in the view and then use a contextual filter to enter the user.

that0n3guy’s picture

I can verify this bug.

To reproduce:

  • Create vocabulary "state"
  • Create vocabulary "city", add entity reference field to this pointing at "state" vocab
  • create some state terms
  • create some city terms, point reference at appropriate city
  • Create a view of taxonomy terms
  • add a contextual filter of term id
  • add a relationship of "Entity Reference: Referencing entity
  • A bridge to the Taxonomy term entity that is referencing Taxonomy term via field_state_reference"
  • save view
  • Create new entity with with entity reference field "city", select widget. This has a Target type of taxonomy term.
  • Use the view you just created.
  • Set the "View arguments" to the tid of one of the states.

The entity add form will only show the last term in the list...

dysrama’s picture

I had this problem as well and figured out what caused it, at least in my case. The entity_reference plugin display assumes the base_table and base_field used as unique key to always be node.nid. When you use relationships in your view, this is not true anymore.
I've attached a patch that makes it possible to define what base_table and what base_id to use when pointing to the view. If you're in doubt what this is in your case, turn on the "view sql" in views settings and check your query.
Hopefully this patch works in other cases than my own.

dysrama’s picture

Just found a minor error in above patch, new one attached

Zorath’s picture

Had the same issue, tried out the patch and it works like a charm.

Thanks a million

noopal’s picture

can somebody upload the patched files, I tried but m not seeing anything different.

Where do I set the base table?

ivfc’s picture

Hello,
Can I kindly request some help with this as well? I applied the above patch and have the same problem as #7, I do not see anything different and am not sure where to define the base table and base id. Could you please clarify how to get this working?
Thank you so much for your help in advance!

JvE’s picture

Status: Active » Needs review
drone.ah’s picture

The base table and fields are on the "edit field" page. However, this does not solve the problem completely.

I have a case where I join a field_collection_item table -> node to another -> field_collection_item table. In this context, this patch cannot help.

In any case, I believe there to be a simpler way of solving the problem. I've run out of time, so will just provide my findings in case it helps someone else.

in entityreference_plugin_style::render(), within the nested for loop, there is a line (:60),

$results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $this->row_plugin->render($values))));

The use of the id_field_alias to build an associative array means that you will get only one result per unique row of the content type of the view is built upon.

In other words,

If you create a view of nodes and there is a filter to limit to to a nid (say 5 for arguments sake), each entry in the view would have the id_field_alias of 5. Each entry would then be put into the same key within the result array and returned.

One way of solving this would be ensure that the relevant id field is output as part of the fields and then reference that in the code. It could perhaps be another field to select within the settings of the display.

Alternatively, restructure the view so that the entity on which the view is built is the one that is output, and not one based on relationships.

I hope this makes sense...

Goofy2k2’s picture

subscribe

drupup’s picture

Issue summary: View changes

Okay, let's say this is my query:

SELECT node_field_data_field_languages.title AS node_field_data_field_languages_title, node.title AS node_title
FROM
{node} node
LEFT JOIN {field_data_field_client} field_data_field_client ON node.nid = field_data_field_client.field_client_target_id AND (field_data_field_client.entity_type = 'node' AND field_data_field_client.deleted = '0')
LEFT JOIN {node} field_client_node ON field_data_field_client.entity_id = field_client_node.nid
LEFT JOIN {field_data_field_client} field_client_node__field_data_field_client ON field_client_node.nid = field_client_node__field_data_field_client.entity_id AND (field_client_node__field_data_field_client.entity_type = 'node' AND field_client_node__field_data_field_client.deleted = '0')
LEFT JOIN {node} node_field_data_field_client ON field_client_node__field_data_field_client.field_client_target_id = node_field_data_field_client.nid
LEFT JOIN {field_data_field_languages} node_field_data_field_client__field_data_field_languages ON node_field_data_field_client.nid = node_field_data_field_client__field_data_field_languages.entity_id AND (node_field_data_field_client__field_data_field_languages.entity_type = 'node' AND node_field_data_field_client__field_data_field_languages.deleted = '0')
LEFT JOIN {node} node_field_data_field_languages ON node_field_data_field_client__field_data_field_languages.field_languages_target_id = node_field_data_field_languages.nid
WHERE (( (field_client_node.nid = '3101' ) )AND(( (node.status = '1') AND (node_field_data_field_languages.type IN ('language')) )))
ORDER BY node_title DESC

In this case, I've got a nested relationship: first to client, then to language, so I can access only the languages that have been selected for that client. Based on this table, what, specifically, would I enter into the "ID field if not nid" and "the base table" fields? I've tried a couple of things, but I'm still getting errors in the edit screen where the view is being rendered, so I'm obviously not using correct syntax.

leeresglas’s picture

Thanks a lot for the patch, but it did only provide a partial solution to my problem which is the one of #10, namely that the wrong field_alias_id is used in a specific context (a view with relationships and contextual filtering). I implemented what drone.ah supposed to do and which works in my actual setting. Besides the fact that all of this is really a hack (one should not expect end-users to enter some database-related stuff anywhere), there is a big problem: it does not work with autocomplete! Since until now a select-list does the job for me, I have not looked further into the query-function which should handle this stuff, but maybe I will in the future.

In my actual setting, I added the content nid to my view (and excluded it from display) and used the field identifier as field_alias_key (or id_field_alias to use the syntax of entity_reference). Adding the base_table and base_field stays necessary, as the reference field would not accept the entries otherwise.

In my impression, the bug which relates to this behavior might be outside of entity reference (but then again, why should the results show up correctly in Views UI, which they do in my case?), however I'm not that familiar with how exactly Views works so I didn't dig that deep into the problem.

leeresglas’s picture

Okay, new stuff: As I had the additional problem, that adding items in a field collection multiple view (see https://drupal.org/node/2198831#comment-8785703) rendered the view items invalid, I had another look into the problem.
It turned out, that the functions getReferencableEntites and validateReferencableEntities don't have access on any arguments which renders results from a view that uses a flexible contextual filter (like node ID from URL) invalid. I therefore wrote a new patch, that references the FieldCollectionEntity in the EntityReference_SelectionHandler_Views and provides the HostEntityID as argument to Views. In my case, this solved the problem that wrong or no results were shown (which renders the patch #13 obsolete, at least in my case) . It also resolved the add another issue (mostly).

robcarr’s picture

Thanks @leeresglas - I'd encountered an issue that I thought was related to Field Collection #2198831: Add another button breaks Entity Reference values. Your patch #14 worked perfectly fine for me - and thanks for flagging up this issue queue. I've not found any other issues with the patch.

The patch gets my vote. More reviews for RTBC please!

inno81’s picture

This patch does not work for me: i have a node with 1 field collection, and 1 field collection nested inside another field collection, both with an entity reference field populated by an entityreference view display (the same for all the ER fields). In particular:

- The non-nested field collection initially shows the options fine. When "add another", the first entry is preserved (doesn't disappear anymore), but the new one is empty.
- The field collection nested in the other field collection is now empty since the beginning (it was initially populated fine without patch).
- The reference field setting page gives this error:

Fatal error: Call to a member function hostEntityId() on a non-object in ...\sites\all\modules\entityreference\plugins\selection\EntityReference_SelectionHandler_Views.class.php on line 109

I applied the patch to latest dev.

biarr’s picture

The patch in 14 does not work for me either. I'm getting the same error

Fatal error: Call to a member function hostEntityId() on a non-object in ...\sites\all\modules\entityreference\plugins\selection\EntityReference_SelectionHandler_Views.class.php on line 109
inno81’s picture

Status: Needs review » Needs work
robcarr’s picture

I'm seeing WSOD for an entity reference field with same fatal error as reported at #16 and #17. However, the [non-node] entity being referenced does not contain any field collections.

Fatal error: Call to a member function hostEntityId() on a non-object in ...\sites\all\modules\entityreference\plugins\selection\EntityReference_SelectionHandler_Views.class.php on line 109

This is the problem code (which is added by the patch at #14):

/**
   * Implements EntityReferenceHandler::getReferencableEntities().
   */
  public function getReferencableEntities($match = NULL, $match_operator = 'CONTAINS', $limit = 0) {
    $display_name = $this->field['settings']['handler_settings']['view']['display_name'];
    $args = $this->field['settings']['handler_settings']['view']['args'];
    if($this->entity->hostEntityId() != NULL) {
      if(is_array($args)) {
        $args[] = $this->entity->hostEntityId();
      } else if(is_string($args)) {
        $args = array($args, $this->entity->hostEntityId());
      } else {
        $args = array($this->entity->hostEntityId());
      }
    }
    $result = array();
    if ($this->initializeView($match, $match_operator, $limit)) {
      // Get the results.
      $result = $this->view->execute_display($display_name, $args);
    }
...

And it's the call to $this->entity->hostEntityId() that's the line 109 flagged up. Which suggests that the patch at #14 has unintended consequences with the Entity Reference module.

hostEntityId() is a function in the Field Collection module to identify the entity hosting the field collection... and this is causing the issue with an entity (object) that is not a field collection.

inno81’s picture

@arrrgh or anyone else: did you manage to fix the issue somehow? Is there any update?

digitgopher’s picture

I found this thread because my view is correct when I preview it, but only shows one result on the node edit page.

I have a content type with 2 entityreference fields. On the second entityreference field I use 'Views: Filter by an entity reference view'. Then in the view I'm reading field_parent (the first entityreference field) in a contextual filter, and then have an 'Entity Reference: Referencing entity' relationship on field_parent. This gives me the results I want when I preview. But then only the last result shows up on the node edit page.

Here is the sql:

SELECT field_parent_node.title AS field_parent_node_title
FROM 
{node} node
INNER JOIN {field_data_field_parent} field_data_field_parent ON node.nid = field_data_field_parent.field_parent_target_id AND (field_data_field_parent.entity_type = 'node' AND field_data_field_parent.deleted = '0')
INNER JOIN {node} field_parent_node ON field_data_field_parent.entity_id = field_parent_node.nid
WHERE (( (field_data_field_parent.field_parent_target_id = '2700' ) ))

I've tried all the patches here, none of them work. The base table issue in #5 doesn't help as my view's base table is node. Field collection issue in #14 only breaks with no field collection here. There has to be a deeper issue here, but I don't know views (or entityreference) well enough to track it down.

Hoping someone can help resolve this!

digitgopher’s picture

Category: Support request » Bug report

Here is what I found after digging into this.

The issue is in entityreference_plugin_style, render function. The key snippet is
$results[$values->{$id_field_alias}] = .......

$results needs to be keyed with the actual node ids. So change $id_field_alias to the nid field *based on the relationship*, and now they have their correct nids rather than all the same.

So this is similar to the base_table issue expressed in #4, except the view output works fine - no need to pass a base table to the view. It's passing the correct table/field info to the style plugin and validation that doesn't work.

1) How to have a generic solution?
2) Validation breaks, how to fix it? Doesn't look like it handles relationships?

dca123’s picture

Subscribe

caldenjacobs’s picture

Like others, my Views Entityreference filter display queries the proper result list, but will only pass one result to the actual EntityReference field calling it. I'm stumped. Anyone have any ideas about how to solve along the lines of #22?

caldenjacobs’s picture

Priority: Normal » Major
Status: Needs work » Active
caldenjacobs’s picture

Uh oh. This *may* actually work as designed.

I was able to get everything working by reversing the way I did things.

I.e.

1. Utilizing a reverse relationship
2. Adding a contextual filter using the reverse relationship

Anyone else have any luck with this approach?

duckydan’s picture

@caldenjacombs That worked for me. Even though I was using a Taxonomy term VBO selector, I had a content view providing the possible results. By reversing it and using a term view that related to the content (instead of a content view relating to terms), it works perfectly now.

It seems odd that doing it the "wrong" way even returned a single result.

Thanks for the tip!

adrianavaz’s picture

hi @maxplus

I know this was 4 years ago but I am having the exact same problem and I can't figure it out...

What do you mean by this:
"my solution was finally very simple: just use the contextual filter right away to filter on the reference field instead of first using a relation in the view and then use a contextual filter to enter the user." ?

I am also trying to display a list of users with a contextual filter. Users that belong to the group (Group Module) of the logged in user.

Thanks!!

caldenjacobs’s picture

Hi @adrianavaz — Could you post an export of your View here?

adrianavaz’s picture

Hi, yes here:

In the view is working just great, all users appear. But when I add the view to the entity reference field, it displays only on value - the last added user.

$view = new view();
$view->name = 'entity_empresas';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'group_membership';
$view->human_name = 'Entity_Empresas';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Entity_Empresas';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'role';
$handler->display->display_options['access']['role'] = array(
  3 => '3',
  2 => '2',
  4 => '4',
);
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Group membership: Group role */
$handler->display->display_options['relationships']['role']['id'] = 'role';
$handler->display->display_options['relationships']['role']['table'] = 'group_membership';
$handler->display->display_options['relationships']['role']['field'] = 'role';
/* Relationship: Group membership: Group gid */
$handler->display->display_options['relationships']['group']['id'] = 'group';
$handler->display->display_options['relationships']['group']['table'] = 'group_membership';
$handler->display->display_options['relationships']['group']['field'] = 'group';
$handler->display->display_options['relationships']['group']['label'] = 'Grupo';
/* Relationship: Group: Group membership */
$handler->display->display_options['relationships']['group_membership']['id'] = 'group_membership';
$handler->display->display_options['relationships']['group_membership']['table'] = 'groups';
$handler->display->display_options['relationships']['group_membership']['field'] = 'group_membership';
$handler->display->display_options['relationships']['group_membership']['relationship'] = 'group';
$handler->display->display_options['relationships']['group_membership']['label'] = 'Membro do grupo';
/* Relationship: Group membership: User uid */
$handler->display->display_options['relationships']['user']['id'] = 'user';
$handler->display->display_options['relationships']['user']['table'] = 'group_membership';
$handler->display->display_options['relationships']['user']['field'] = 'user';
$handler->display->display_options['relationships']['user']['relationship'] = 'group_membership';
$handler->display->display_options['relationships']['user']['label'] = 'Utilizador';
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'user';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['element_label_colon'] = FALSE;
/* Contextual filter: Group membership: User uid */
$handler->display->display_options['arguments']['user']['id'] = 'user';
$handler->display->display_options['arguments']['user']['table'] = 'group_membership';
$handler->display->display_options['arguments']['user']['field'] = 'user';
$handler->display->display_options['arguments']['user']['relationship'] = 'group_membership';
$handler->display->display_options['arguments']['user']['default_action'] = 'default';
$handler->display->display_options['arguments']['user']['exception']['title'] = 'Todos';
$handler->display->display_options['arguments']['user']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['user']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['user']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['user']['summary_options']['items_per_page'] = '25';
/* Filter criterion: Group membership: Group role */
$handler->display->display_options['filters']['role']['id'] = 'role';
$handler->display->display_options['filters']['role']['table'] = 'group_membership_role';
$handler->display->display_options['filters']['role']['field'] = 'role';
$handler->display->display_options['filters']['role']['relationship'] = 'group_membership';
$handler->display->display_options['filters']['role']['operator'] = 'empty';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'entity-empresas';

/* Display: Referência de Entidade */
$handler = $view->new_display('entityreference', 'Referência de Entidade', 'entityreference_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'entityreference_style';
$handler->display->display_options['style_options']['search_fields'] = array(
  'name' => 'name',
);
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['row_plugin'] = 'entityreference_fields';
$handler->display->display_options['row_options']['inline'] = array(
  'name' => 'name',
);
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['arguments'] = FALSE;
/* Contextual filter: Group membership: User uid */
$handler->display->display_options['arguments']['user']['id'] = 'user';
$handler->display->display_options['arguments']['user']['table'] = 'group_membership';
$handler->display->display_options['arguments']['user']['field'] = 'user';
$handler->display->display_options['arguments']['user']['default_action'] = 'default';
$handler->display->display_options['arguments']['user']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['user']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['user']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['user']['summary_options']['items_per_page'] = '25';
$translatables['entity_empresas'] = array(
  t('Master'),
  t('Entity_Empresas'),
  t('more'),
  t('Apply'),
  t('Reset'),
  t('Sort by'),
  t('Asc'),
  t('Desc'),
  t('Membership role'),
  t('Grupo'),
  t('Membro do grupo'),
  t('Utilizador'),
  t('Uid'),
  t('Todos'),
  t('Page'),
  t('Referência de Entidade'),
  t('All'),
);

Thanks!

caldenjacobs’s picture

Thanks @adrianavaz!

As discussed, it seems that because of the nature of how Entityreference interprets Views results, we must Contextually filter using Relationships instead of using Fields dependent directly on Relationships in our Entityreference Views displays.

See the following example, which with some modification might work for your use case:

$view = new view();
$view->name = 'entity_empresas';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'group_membership';
$view->human_name = 'Entity_Empresas';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Entity_Empresas';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'role';
$handler->display->display_options['access']['role'] = array(
  3 => '3',
  2 => '2',
  4 => '4',
);
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Group membership: Group gid */
$handler->display->display_options['relationships']['group']['id'] = 'group';
$handler->display->display_options['relationships']['group']['table'] = 'group_membership';
$handler->display->display_options['relationships']['group']['field'] = 'group';
$handler->display->display_options['relationships']['group']['label'] = 'Grupo';
/* Relationship: Group: Group membership */
$handler->display->display_options['relationships']['group_membership']['id'] = 'group_membership';
$handler->display->display_options['relationships']['group_membership']['table'] = 'groups';
$handler->display->display_options['relationships']['group_membership']['field'] = 'group_membership';
$handler->display->display_options['relationships']['group_membership']['relationship'] = 'group';
$handler->display->display_options['relationships']['group_membership']['label'] = 'Membro do grupo';
/* Relationship: Group membership: User uid */
$handler->display->display_options['relationships']['user']['id'] = 'user';
$handler->display->display_options['relationships']['user']['table'] = 'group_membership';
$handler->display->display_options['relationships']['user']['field'] = 'user';
$handler->display->display_options['relationships']['user']['relationship'] = 'group_membership';
$handler->display->display_options['relationships']['user']['label'] = 'Utilizador';
/* Field: Group membership: Rendered Group membership */
$handler->display->display_options['fields']['rendered_entity']['id'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['table'] = 'views_entity_group_membership';
$handler->display->display_options['fields']['rendered_entity']['field'] = 'rendered_entity';
$handler->display->display_options['fields']['rendered_entity']['label'] = '';
$handler->display->display_options['fields']['rendered_entity']['alter']['alter_text'] = TRUE;
$handler->display->display_options['fields']['rendered_entity']['alter']['trim_whitespace'] = TRUE;
$handler->display->display_options['fields']['rendered_entity']['alter']['strip_tags'] = TRUE;
$handler->display->display_options['fields']['rendered_entity']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['element_default_classes'] = FALSE;
$handler->display->display_options['fields']['rendered_entity']['link_to_entity'] = 0;
$handler->display->display_options['fields']['rendered_entity']['view_mode'] = 'full';
$handler->display->display_options['fields']['rendered_entity']['bypass_access'] = 0;
/* Contextual filter: Group membership: User uid */
$handler->display->display_options['arguments']['user_1']['id'] = 'user_1';
$handler->display->display_options['arguments']['user_1']['table'] = 'group_membership';
$handler->display->display_options['arguments']['user_1']['field'] = 'user';
$handler->display->display_options['arguments']['user_1']['relationship'] = 'group_membership';
$handler->display->display_options['arguments']['user_1']['default_action'] = 'default';
$handler->display->display_options['arguments']['user_1']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['user_1']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['user_1']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['user_1']['summary_options']['items_per_page'] = '25';

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['path'] = 'entity-empresas';

/* Display: Referência de Entidade */
$handler = $view->new_display('entityreference', 'Referência de Entidade', 'entityreference_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'entityreference_style';
$handler->display->display_options['style_options']['search_fields'] = array(
  'rendered_entity' => 'rendered_entity',
);
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['row_plugin'] = 'entityreference_fields';
$handler->display->display_options['row_options']['inline'] = array(
  'name' => 'name',
);
$handler->display->display_options['defaults']['row_options'] = FALSE;
$handler->display->display_options['defaults']['arguments'] = FALSE;
/* Contextual filter: Group membership: User uid */
$handler->display->display_options['arguments']['user']['id'] = 'user';
$handler->display->display_options['arguments']['user']['table'] = 'group_membership';
$handler->display->display_options['arguments']['user']['field'] = 'user';
$handler->display->display_options['arguments']['user']['relationship'] = 'group_membership';
$handler->display->display_options['arguments']['user']['default_action'] = 'default';
$handler->display->display_options['arguments']['user']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['user']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['user']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['user']['summary_options']['items_per_page'] = '25';

adrianavaz’s picture

Hi, thank you for this.

It appears like this:

Member <em class="placeholder">admin</em> in <em class="placeholder">Empresas grupo 1</em>
Member <em class="placeholder">Empresa A</em> in <em class="placeholder">Empresas grupo 1</em>
Member <em class="placeholder">Empresa A1</em> in <em class="placeholder">Empresas grupo 1</em>
Member <em class="placeholder">Empresa A2</em> in <em class="placeholder">Empresas grupo 1</em>
Member <em class="placeholder">Empresa A3</em> in <em class="placeholder">Empresas grupo 1</em>

But it doesn't do what the other view was doing:

1. I just want "Empresa A1" to appear not all of the "Member Empresa A1 in Empresas grupo 1"
If I add Name in the fields only the logged in user name appears.

2. I need the filter "Only has the member role" to be active but when I do that the results disappear.

Do you know how to work around this?

Thanks!

adrianavaz’s picture

Ok I managed to get the 2. to work.

But I still need this:
"1. I just want "Empresa A1" to appear not all of the "Member Empresa A1 in Empresas grupo 1"

In the display these are the options:

http://imgur.com/a/zF4N4

caldenjacobs’s picture

Ah, apologies to all for (sort of) hijacking the thread — perhaps at this point it might be best to move this discussion to the Group module's queue. At the same time, hopefully documenting how to alternatively query Views using Contextual filters on top of Relationships will be helpful to other folks researching this rendering issue.

@adrianavaz — it looks as if you'd like to reference Users directly, instead of Group Memberships. This being the case, you'll want to create a new View, and set it to query the User table instead of Group Memberships. I'll provide an export below as an example, but you'll have to reapply your role filters and other custom settings as I created this from scratch.

You'll also likely need to create a new entityreference field, and be sure Users are selected as the Target Type, and then you'll be able to filter through this new View:

$view = new view();
$view->name = 'user_name_s_from_group_membership';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'users';
$view->human_name = 'User Name(s) from Group Membership';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['access']['perm'] = 'access user profiles';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: User: Group membership */
$handler->display->display_options['relationships']['group_membership']['id'] = 'group_membership';
$handler->display->display_options['relationships']['group_membership']['table'] = 'users';
$handler->display->display_options['relationships']['group_membership']['field'] = 'group_membership';
/* Relationship: Group membership: Group gid */
$handler->display->display_options['relationships']['group']['id'] = 'group';
$handler->display->display_options['relationships']['group']['table'] = 'group_membership';
$handler->display->display_options['relationships']['group']['field'] = 'group';
$handler->display->display_options['relationships']['group']['relationship'] = 'group_membership';
/* Relationship: Group: Group membership */
$handler->display->display_options['relationships']['group_membership_1']['id'] = 'group_membership_1';
$handler->display->display_options['relationships']['group_membership_1']['table'] = 'groups';
$handler->display->display_options['relationships']['group_membership_1']['field'] = 'group_membership';
$handler->display->display_options['relationships']['group_membership_1']['relationship'] = 'group';
/* Relationship: Group membership: User uid */
$handler->display->display_options['relationships']['user']['id'] = 'user';
$handler->display->display_options['relationships']['user']['table'] = 'group_membership';
$handler->display->display_options['relationships']['user']['field'] = 'user';
$handler->display->display_options['relationships']['user']['relationship'] = 'group_membership_1';
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['label'] = '';
$handler->display->display_options['fields']['name']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['name']['alter']['ellipsis'] = FALSE;
/* Sort criterion: User: Created date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'users';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Contextual filter: Group membership: User uid */
$handler->display->display_options['arguments']['user']['id'] = 'user';
$handler->display->display_options['arguments']['user']['table'] = 'group_membership';
$handler->display->display_options['arguments']['user']['field'] = 'user';
$handler->display->display_options['arguments']['user']['relationship'] = 'group_membership_1';
$handler->display->display_options['arguments']['user']['default_action'] = 'default';
$handler->display->display_options['arguments']['user']['default_argument_type'] = 'current_user';
$handler->display->display_options['arguments']['user']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['user']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['user']['summary_options']['items_per_page'] = '25';

/* Display: Entity Reference */
$handler = $view->new_display('entityreference', 'Entity Reference', 'entityreference_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['pager']['type'] = 'none';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'entityreference_style';
$handler->display->display_options['style_options']['search_fields'] = array(
  'name' => 'name',
);
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['row_plugin'] = 'entityreference_fields';
$handler->display->display_options['defaults']['row_options'] = FALSE;

Cheers!

Ramirez’s picture

This is just a workaround.
Using https://www.drupal.org/project/entityreference_view_widget as a widget, the problem does not occur.
Obviously, it does not solve the original long pending issue. However, you may give it a try as a last resort.

adrianavaz’s picture

Thank you all.

@caldenjacobs it worked! thanks

Ramirez’s picture

After a long and demanding search, I resolved the issue for my project regarding OG group and members.
To summarize it up: in views you should not use the relationships directly in the fields. Instead, you should use contextual filters to narrow the list according to the needed result.
So you should use relationship + contextual filters (and NOT relationship directly in fields).

rakesh.nimje84@gmail.com’s picture

I was facing the same issue.
After trying the tweak proposed in #37, it started working.

electrokate’s picture

For Drupal 8, following the guide for Business Rules here, using entities instead of taxonomy, this issue is still happening. I tried using the entity reference views select module with the patch that is applied specifically for this issue and the problem remains. Does anyone have any insight?

edited:
@caldenjacobs I have tried add a relationship and contextual filter as you described above but it does not work, it stops pulling any values rather than just 1.
-my view with just a contextual filter of "Content: Title" works but the select list is populated only with one of the multiple values displayed in the view preview
-adding a relationship of "Content referenced by the parent field" and then adding the contextual filter of Title based on that relationship then pulls in no values.
-tried ENTITY REFERENCE VIEWS SELECT module for Drupal 8, but get the same results. There is a patch in that module for this issue but it doesn't appear to work.
-the multiple fields that are not all showing up are Paragraphs fields

electrokate’s picture

@drone.ah

The Drupal 8 render function in Views (EntityReference.php) is this:

  /**
   * {@inheritdoc}
   */
  public function render() {
    if (!empty($this->view->live_preview)) {
      return parent::render();
    }

    // Group the rows according to the grouping field, if specified.
    $sets = $this->renderGrouping($this->view->result, $this->options['grouping']);

    // Grab the alias of the 'id' field added by
    // entity_reference_plugin_display.
    $id_field_alias = $this->view->storage->get('base_field');

    // @todo We don't display grouping info for now. Could be useful for select
    // widget, though.
    $results = [];
    foreach ($sets as $records) {
      foreach ($records as $values) {
        $results[$values->{$id_field_alias}] = $this->view->rowPlugin->render($values);
        // Sanitize HTML, remove line breaks and extra whitespace.
        $results[$values->{$id_field_alias}]['#post_render'][] = function ($html, array $elements) {
          return Xss::filterAdmin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $html)));
        };
      }
    }
    return $results;
  }

It is still referencing the $id_field_alias like you mentioned above (#10) for Drupal 7:.

$results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $this->row_plugin->render($values))));

Perhaps a patch for this would fix this issue in Drupal 8? Is their comment about not displaying grouping info relevant to this issue?

felubra’s picture

I was able to make it work using the approach of #26 , thanks @caldenjacobs!