Thanks for the very useful module. But I have an idea that can make it even more useful.
How about we allow URI path components to provide entity-ids?
Let's say I have content type 'foo' with a field named 'field_other_user' which is user entity reference. Currently I have create my node at the path /node/add/foo?field_other_user=123, such that user with uid 123 will get pre-populated in the 'field_other_user' field.
But what if I had some means of adding a menu-item, tab (local task) or local action on the user's profile at path 'user/123/other' that somehow points to the node/add/foo form? (I can do this in code, or using panel & page_manager) In this situation, I would like to pre-populate field_other_user of content type foo with the %user uid in 'user/%user/other' path.
I have made small code changes to make the above possible. Basically, I added an extra option to the behavior settings that indicates which (if any) part of the URI path component provides the entity id(s). And then at runtime, while extracting the ids, I look at that setting to decide whether to extract the ids from the URI path component or from query string (which is the default behavior).
I will attach the patch in the next comment.
Comment | File | Size | Author |
---|---|---|---|
#1 | entityreference_prepopulate-entity_id_from_path-1981430-1.patch | 2.37 KB | Jitesh Doshi |
Comments
Comment #1
Jitesh Doshi CreditAttribution: Jitesh Doshi commentedThe patch for the above feature request is attached.
Thanks --Jitesh
Comment #2
Jitesh Doshi CreditAttribution: Jitesh Doshi commentedBy the way, with the above patch, I have been using this module in conjunction with panels & page_manager beautifully to create local actions and local tasks (tabs) on user profile pages (user/%user/something) to create nodes with those users pre-populated in an entityreference field.
Here's the general process:
Now when you visit the user profile, you should see a link to user/%user/something which creates a node of the new custom content type with the said user pre-populated in it.
All of this without writing a single line of code! Cool, eh?
Comment #3
vflirt CreditAttribution: vflirt commentedHi,
I needed exactly this functionality and it works like a charm. So far I cannot think of any other more suitable way to implement this functionality.
Kind Regards,
Dobromir
Comment #4
osopolarTo me this seems to be more like a hack. What about pre-populating two or more fields or "x/y/z/p/q/r/%id"?
I guess, this could be done instantly without patching. The page "user/%user/something" could be a redirect action (page-manager alternative to panel action) and redirect to node/add/{type}?[field_ref]=[id].
A more elegant way would be pre-populating the entity reference by page-manager context. Maybe it already does this, I did not check.
Comment #5
13rac1 CreditAttribution: 13rac1 commentedI like the idea of this patch.
"x/y/%field_name_a/z/%field_name_b" The form %user is generally used to run Auto-loader Wildcards, but could be used here. See: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/func...
Comment #6
amitaibuSince version 1.5 You can now have your own prepopulating providers, by implementing
hook_entityreference_prepopulate_providers_info()
, so your code can leave in conrtib.