This is a biggie. I'd like to be able to prepopulate node EDIT forms as well. For example, I'm currently making a sort of curated gallery node that contains entity references to entities of type "gallery_item." I'd like to be able to do this:
- A user is looking at a "gallery_item" entity
- She clicks a link from that entity that takes her to a page that says "Which gallery would you like to add this item to?" with a list of existing gallery nodes.
- She clicks one of the links, and it takes her to the node edit form of that gallery, with the gallery_item's entity ID in the URL, prepopulating the multi-value entity reference field in the edit form. (Important: this should not cause problems with existing entity references! i.e., the node's existing entity references should appear in the edit form, and the entities passed in via the URL should be ADDED to them if the entity reference field is multi-value.)

What happens now is that the entity ID's passed in via the URL are completely ignored in an edit form.

Thanks!

Comments

rogical’s picture

Needs the same!

rbruhn’s picture

I am needing this same functionality too. In looking at the code, it appears this module adds a default_value_function to the instance of the field on which you want this behavior. The default_value_function is called within field.form.inc function field_default_form():

//.....
// Populate widgets with default values when creating a new entity.
if (empty($items) && empty($id)) {
  $items = field_get_default_value($entity_type, $entity, $field, $instance, $langcode);
}
//.....

At this point, a node being edited might already have values for $items and the $id. So the default_value_function will never be called.

It seems like adding url values to a entity reference field would need to be handled in a form alter function inside a module. I've been playing around with it most of the day, but damn it's a bit difficult. It would be nice to intercept the data for the form build, check for the existing url values, and add them to the data before the form is built and displayed. If anyone has ideas on how to do that, I'd be interested in hearing them.

rbruhn’s picture

For others who may look here, I ended up simply using hook_node_load() to add what I needed.

/**
 * Implements hook_node_load()
 * Adds url values to edit node form 
 */
function my_module_node_load($nodes, $types) {
  if (!in_array('my_node_type', $types)) 
    return;
  if (!isset($_GET['my_entity_reference_field'])) 
    return;
  
  $items = explode(',', $_GET['my_entity_reference_field']);
  
  foreach ($nodes as $node) {
    foreach ($items as $item) {
      $node->my_entity_reference_field[LANGUAGE_NONE][] = array('target_id' => $item);
    }
  }
}

This simply adds the url values to the entity reference field before the form is displayed so everything is taken care of. Of course, if you are using hook_node_load() for other things, your code would exit differently.

johankasperi’s picture

Thanks rbruhn! Really needed that code of yours :) But I also need something similar for User edit, and I don't have the skillset to write it on my own. Can you help? Thanks in advance!

rbruhn’s picture

@Kasberry
Just came back to look at this. It depends on what you are trying to do in the User edit. If you can explain, perhaps I can lend a hand.

johankasperi’s picture

@rbruhn The request I posted a few days ago is no longer needed since we found an other solution to our problem. Thanks anyhow!

jan-e’s picture

I came here looking for a way to change an existing entity reference, not to add one to an existing multi-value entity reference field. If I skip the multi, I ended up using the prepopulate module in stead of the entity reference prepopulate module. My my_entity_reference_field was in a select list and I used this to populate it with the desired value:

/node/xxx/edit?edit[my_entity_reference_field][und]=my_entity_id

I found the solution here: http://drupal.org/node/1772148

For the multi case something like this might be worth a try:

/node/xxx/edit?edit[my_entity_reference_field][und][]=my_entity_id

windmaomao’s picture

need this one as well, pump

nwom’s picture

Issue summary: View changes
Status: Active » Closed (fixed)

This functionality exists in the current dev version of the module (not sure about the stable). The only exception, is when using this in combination with the Entity Reference Prepopulate Token module. That would have to be patched in the Entity Reference Prepopulate Token module though.

lunk rat’s picture

Version: 7.x-1.0 » 7.x-1.x-dev
Status: Closed (fixed) » Needs work

On the latest dev, visiting /node/1/edit?field_foo=2 does not prepopulate the entity reference field_foo

Is there any other config I should be aware of?

ludwig.rubio’s picture

#3 works perfectly!, thanks!