Here is a patch that provides a drupal_alter hook to change the arguments that are set to the View.

stella started his patch and then I updated. It is from the issue #1626668: Provide referencing entity as views argument

I wanted this hook so I could add the entity id of the entity that the field is attached to to be set as an argument. It could be used for many other reasons such as to pass the entity language as an argument.

Here is an example of my implementation

Here is the implementation that I needed to pass the Current nid as the second argument.

function modulename_entityreference_view_widget_views_arguments_alter(&$arguments, $context) {
  $node = $context['element']['#entity'];
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
  if (!empty($node) && !empty($node->nid)) {
    $arguments[] = $node->nid;
  }
}

This way if the nid is available it is always the second argument and by passing 'all' as the first argument the View can will work even if there are no currently selected entities.

Comments

supermoos’s picture

That seems like a very good starting point, I have it working, but how do I use the argument to filter values in the entity reference view widget?

tedbow’s picture

@supermoos,
Not sure about your question. Are you asking how you add a nid contextual filter to you View?

supermoos’s picture

@tedbow
No, I am talking about how I would pass a language value to the view, and filter out all node's that do not have an entity translation for the given language?

mistermastermatt’s picture

Hello, do you have any idea how I would need to adapt your code to get it to work inside a 'field collection' item? As provided by the field collections module. It feels like it shouldn't be too hard but I've had no luck.
Thanks.

Edit: The closest I've got is the following:

function custom_nid_passer_entityreference_view_widget_views_arguments_alter(&$arguments, $context) {
  $node = $context['element']['#entity'];
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
if ($node = menu_get_object()) {
  $nid = $node->nid;
}
  if (!empty($node) && !empty($nid)) {
    $arguments[] = $nid;
  }
}

Which works once, in other words it works on page load but not after selecting an item.
As you can see, I haven't got a clue what I'm doing.
I would really appreciate any help.
Thanks again.

Bitvark’s picture

Hello,
for field collection item (#4) you can change your override function as follow.
First you need to install the patch in this post and to set the second contextual filter with the proper relationship in the view, obviously.

function modulename_entityreference_view_widget_views_arguments_alter(&$arguments, $context) {
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
  if ($context['entity_type']!="field_collection_item"){
    $node = $context['element']['#entity'];
    if (!empty($node) && !empty($node->nid)) {
      $arguments[] = $node->nid;
    }
  } else {
    $arguments[]=$context['element']['#entity']->hostEntityId();
  }
}
mistermastermatt’s picture

Thanks very much for your response, it is very much appreciated.
Unfortunately, I haven't quite got this working yet. I'm pretty confident I have everything set up correctly as when not using a Field Collection the OP's code works as expected.
With your code, what is actually being passed as the second argument? Is it the NID the field collection is on, or is it the entity ID of the field collection item?
Many thanks,
Matt

Bitvark’s picture

Hi, the code at #5 passes the NID the field collection is on.

Try
drupal_set_message("<pre>".print_r($context,1)."</pre>");
before the if-statement, and before end of function paste this:
drupal_set_message("<pre>".print_r($arguments,1)."</pre>");

This way we can understand where the error is.

In the view, you have to use a relationship to the "Field collection item - field_COLLECTION_NAME".
The second argument, in the view is: "(field_COLLECTION_NAME) Content: Nid".

Let us know if it works please.

mistermastermatt’s picture

StatusFileSize
new5.65 KB
new20.9 KB
new39.21 KB
new78.75 KB

Ok, sorry for absence and many thanks again for your help on this.
I've attached some bits and pieces. 1.txt is the result of your drupal_set_message 's that appears at the top of the page.
2.txt appears in the view window select area (see 2image.jpeg) after making a selection.

When I first did this I noticed the line hostEntityId:protected and that the second argument wasn't being set - gleaned from the second drupal_set_message. On a whim I went into field_collection.module and changed the line: protected $hostEntiyId; to public $hostEntityId; and got all excited as I saw that the second argument was now being set as in 1.txt but wasn't in 2.txt. Also, it didn't work, all entities were being displayed like the view didn't get the nid. I then changed that line in field_collection.module back to what it was originally and to my surprise found that the argument was still being set - it didn't revert back to how things were at first.
At the moment. field_collection.module is back to unblemished state. drupal_set_messages are as described in attachments. It still doesn't work.

I have double checked that when not using a field collection everything works fine. I attach the view for good measure.

Very many thanks,
Matt

mistermastermatt’s picture

Aha, success!
I fiddled around with your code a little and noticed that the else statement was out on its own.

function modulename_entityreference_view_widget_views_arguments_alter(&$arguments, $context) {
//drupal_set_message("<pre>".print_r($context,1)."</pre>");
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
  if ($context['entity_type']!="field_collection_item"){
    $node = $context['element']['#entity'];
    if (!empty($node) && !empty($node->nid)) {
      $arguments[] = $node->nid;
    }
  else {
    $arguments[]=$context['element']['#entity']->hostEntityId();
  }
  }
//    drupal_set_message("<pre>".print_r($arguments,1)."</pre>");  
}

Again, thanks so much for taking time to help me out on this.
You make Drupal brilliant!
Matt

mistermastermatt’s picture

StatusFileSize
new96.23 KB

Spoke too soon, but so close. The problem now is 'Add another item'. The first field collection item is now working perfectly. However, when I add another field collection item the nid is no longer passed. Please see attached image.
Thanks,
Matt

Bitvark’s picture

I'm having similar problem right now, and solved it maybe.

Apply the patch attached to a new 7.x-1.x-dev version of the module.
(I've created the patch starting from stella's code and tedbow' s one on this page.)

Your module function then can be the following.

function mymodule_entityreference_view_widget_views_arguments_alter(&$arguments, $context) {
  if (empty($arguments)) {
    $arguments[0] = 'all';
  }
  if ($context['element']['#entity_type']!="field_collection_item"){
    if ($context['element']['#entity_type']=="node"){
      $node = $context['element']['#entity'];
      if (!empty($node) && !empty($node->nid)) {
        $arguments[] = $node->nid;
      }
    }
  } else {
    if ((isset($context['element']['#entity']))&&($context['element']['#entity']->hostEntityId()!='')){
      $arguments[]=$context['element']['#entity']->hostEntityId();
    } else if ($context['node_ref_nid']!=0) $arguments[]=$context['node_ref_nid'];
  }
}

I've not tested this enought yet. If you find some bugs let me know please.

mistermastermatt’s picture

Brilliant, that seems to have done it.
Really wasn't so simple after all it seems.
Again, I can't thank you enough, and if I find any bugs I shall certainly post them here.
One last thing, in my case I had to remember to re-apply this (the patch at #3) to get everything working.
Thanks again, and sorry for the seriously belated response.
Matt

das-peter’s picture

For me the patch works as expected so far (still testing).
While reviewing it I found following coding standard nitpicks:

  1. +++ b/entityreference_view_widget.module
    @@ -208,14 +208,22 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +  ¶
    
    @@ -226,14 +234,34 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +  ¶
    +  // Add node form NID for altering $arguments to the View ¶
    ...
    +  	  $node_ref_nid= $form_state['node']->nid;
    ...
    +  ¶
    

    Whitespace issues (trailing whitespace or tab instead spaces).
    $node_ref_nid= $form_state['node']->nid; should have a space before "=".

  2. +++ b/entityreference_view_widget.module
    @@ -226,14 +234,34 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +  // on field collectionon a node ¶
    

    Inline comments must end in full-stops, exclamation marks, or question marks.

  3. +++ b/entityreference_view_widget.module
    @@ -226,14 +234,34 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +  $node_ref_nid=0;
    

    Expected a space before and after "=".

  4. +++ b/entityreference_view_widget.module
    @@ -253,14 +281,14 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +    '#parents' =>  array_merge($element['#field_parents'], array($field_name, 'page')),
    
    @@ -296,12 +326,14 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +          '#parents' =>  array_merge($element['#field_parents'], array($field_name, $field_language, 'items', $entity_id, '_weight')),
    
    @@ -309,14 +341,14 @@ function entityreference_view_widget_field_widget_form(&$form, &$form_state, $fi
    +          '#parents' =>  array_merge($element['#field_parents'], array($field_name, $field_language, 'items', $entity_id, 'remove')),
    

    Expected 1 space after "=>"; 2 found

finex’s picture

Issue summary: View changes

I'm really interested on this task, mainly for the language support.

I've a question about new nodes: when you create a new node the language is not yet set, would Entity Reference View Widget be able to respond to the language selected by the user?

  1. Open /node/add/[CONTENT-TYPE]
  2. Select the node language [LANG]
  3. Open Entity Reference View Widget passing the selected [LANG] even if the node has not yet been saved.

What do you think about?

jsacksick’s picture

Version: 7.x-1.x-dev » 7.x-2.x-dev
Status: Needs review » Closed (duplicate)

This hook has been added in the 2.x branch and a few feature requests were opened about it, feel free to open a new issue if it's not working as expected.