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.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Jitesh Doshi’s picture

The patch for the above feature request is attached.

Thanks --Jitesh

Jitesh Doshi’s picture

By 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:

  1. Download and enable panels, page_manager, entityreference and entityreference_prepopulate modules.
  2. Create a custom content type that has an entityreference field pointing to a user entity.
  3. In page_manager, create a custom page with path 'user/%user/something', and make that page a local action or local task (tab).
  4. Because you used %user in the path, page_manager will let you create a context containing the user referred by %user. So do that.
  5. Now at another custom context of type 'node add form' and choose the custom content type you created above
  6. Proceed to add the above node add form to the page and, if you with, the user profile of the user as well.
  7. Update and Save everything

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?

vflirt’s picture

Hi,

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

osopolar’s picture

+++ b/plugins/behavior/EntityReferencePrepopulateInstanceBehavior.class.php
@@ -8,6 +8,20 @@ class EntityReferencePrepopulateInstanceBehavior extends EntityReference_Behavio
+        5 => t('URI path component 5 (x/y/z/p/q/%id)'),
...
+    );

To 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.

13rac1’s picture

Issue summary: View changes

I like the idea of this patch.

What about pre-populating two or more fields or "x/y/z/p/q/r/%id"?

"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...

amitaibu’s picture

Status: Active » Closed (won't fix)

Since 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.