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.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | views-arguments-field-collection-multiple-1978084-11.patch | 12.62 KB | Bitvark |
| #10 | multi-colection.png | 96.23 KB | mistermastermatt |
| #8 | 2image.jpeg | 78.75 KB | mistermastermatt |
| #8 | 1.txt | 39.21 KB | mistermastermatt |
| #8 | 2.txt | 20.9 KB | mistermastermatt |
Comments
Comment #1
supermoos commentedThat 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?
Comment #2
tedbow@supermoos,
Not sure about your question. Are you asking how you add a nid contextual filter to you View?
Comment #3
supermoos commented@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?
Comment #4
mistermastermatt commentedHello, 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:
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.
Comment #5
Bitvark commentedHello,
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.
Comment #6
mistermastermatt commentedThanks 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
Comment #7
Bitvark commentedHi, 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.
Comment #8
mistermastermatt commentedOk, 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
Comment #9
mistermastermatt commentedAha, success!
I fiddled around with your code a little and noticed that the else statement was out on its own.
Again, thanks so much for taking time to help me out on this.
You make Drupal brilliant!
Matt
Comment #10
mistermastermatt commentedSpoke 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
Comment #11
Bitvark commentedI'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.
I've not tested this enought yet. If you find some bugs let me know please.
Comment #12
mistermastermatt commentedBrilliant, 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
Comment #13
das-peter commentedFor me the patch works as expected so far (still testing).
While reviewing it I found following coding standard nitpicks:
Whitespace issues (trailing whitespace or tab instead spaces).
$node_ref_nid= $form_state['node']->nid;should have a space before "=".Inline comments must end in full-stops, exclamation marks, or question marks.
Expected a space before and after "=".
Expected 1 space after "=>"; 2 found
Comment #14
finex commentedI'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?
What do you think about?
Comment #15
jsacksick commentedThis 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.