After a user add new content using the prepopulate link, on submit, returned user to the referenced entity display instead of the newly added node view. See attached node edit screen shot for a reference field using the Node URL Widget for the References module.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amitaibu’s picture

I'm ok with adding this. A patch would get it quicker than a screenshot ;)

rwilson0429’s picture

Thanks Amitaibu. I'm so sorry that I am not a coder. Otherwise, I would have definitely provided a patch. I'm trying to learn though. It (coding) seem so damn complicated to me. From where I sit, you guys are genius. Hopefully, I'll begin to grasp it one day and be able to contribute patches.

rokr’s picture

Workaround: you can do this with rules very easy.

Nchase’s picture

Yeah, rules might work on a simple setup.

The desitination should be configurable from the field itself, or the ?destination=node/nid parameter should work...

amitaibu’s picture

> The desitination should be configurable from the field itself

I agree. How about a patch? :)

Nchase’s picture

:) we have to check the old url widget and get familiar with the prepopulate module. give us some time :)

trailrunner’s picture

As far as the Rules workaround, can you explain that a bit more? I'm just wondering how you set up Rules to do so.

Thanks!

rwilson0429’s picture

I was able to use drupal_get_destination() to redirect the user to the referenced entity. According to the api documentation:

Use drupal_get_destination() function to direct the user back to the referring page after completing a form. See http://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_get_destination/7 . If a destination exists in the previous request, that destination is returned. As such, a destination can persist across multiple pages.

Entity Reference Prepopulate requires a link that looks something like this: node/add/article?[field_ref]=[id]. So, a link to add new content and prepopulate the entity reference field would look something like:

<a href="/node/add/[YOUR-CONTENT-TYPE-MACHINE-NAME]?[YOUR-ER-FIELD]=[nid]">Add a [YOUR-CONTENT-TYPE]</a>

Where [YOUR-ER-FIELD] is the name of the entity reference field and [nid] is the nid of the entity being referenced.

The entity reference prepopulate parameter and the drupal_get_destination() function can be used together to prepopulate the ER field and return the user to the referenced entity after completing the node/add/[content-type] form. This can be accomplished with Views and EVA (Entity Views Attach) modules.

Assuming Views module is already installed, install and enable the EVA module.
Create a View with an Entity Content display of child entities (for example, images) and configure the Entity Content display Settings as follows:

Entity content settings

Entity type: Node
Bundles: [THE-ENTITY-TYPE-BEING-REFERENCED]
Arguments: id

Contextual filters

Content: [Your-ENTITY-REFERENCE-FIELD] (configure the filter and validate by setting the Filter value format to 'Node ID')

Relationships

Content entity referenced from [YOUR-REFERENCE-FIELD] (this relationship is used to get the nid of the referenced entity. Add the content nid to your View field list and use this relationship)

In the View's Header or Footer, add a Global Text Area and set the text filter to PHP.
Insert the code below

<?php
global $base_url;
$destination = drupal_get_destination();
$url = $base_url . '/node/add/[YOUR-CHILD-CONTENT-TYPE]?field_gallery_reference=[nid]&';

$options = array(
  'query' => drupal_get_destination(),
  'attributes' => array(
    'class' => array('add-new'),
    'id' => 'add-new-[YOUR-CHILD-CONTENT-TYPE]',
  ),
);
print l(t('Add new Media'), $url, $options);
?>

Now when you are viewing a parent content type, you will have a link to add a child content type. Clicking the link should take you to the node/add form for the child content and the entity reference field should be prepopulated. When you save the child content, you should be redirected to the the parent.

I'm sure this could be accomplished in a more elegant manner, but it worked for me.

Jason Dean’s picture

I worked around this using a Rule with these components:

  • Event: after saving new content, after updating existing content
  • Conditions: content is of type [the referencing content type]
  • Actions: page redirect with parameter [node:reference field:url]

This means the redirect happens even if prepopulate isn't used (e.g. the user manually adds new content) but that didn't matter in my use case.

jastraat’s picture

Issue summary: View changes
FileSize
1.67 KB

This may be partially a bug. In looking at entityreference_prepopulate_create_node_links(), there is a parameter for $destination, but the variable is never used in the actual function. Instead, drupal_get_destination() is used.

$options = array(
    'query' => array($field_name => $entity_id) + drupal_get_destination(),
  );

Also, seeing the created post after node add is the default way drupal operates, so it seems like there should be an option to not append a destination parameter.

I've attached a patch that changes the entityreference_prepopulate_create_node_links() function to actually use the $destination parameter passed. The default behavior remains the same, but with this patch it is possible to set a custom destination or no destination at all.

jastraat’s picture

Title: Automatically return user to referenced entity » $destination parameter not used by content create links function
Category: Feature request » Bug report
Status: Active » Needs review
jastraat’s picture

Here's a second patch that incorporates the changes to entityreference_prepopulate_create_node_links() to allow passing a destination parameter along with a small change to the content_type ctools plugin to give the option to not append a destination parameter.

jastraat’s picture

@Amitaibu any chance you could review the attached patch?

tory-w’s picture

@ Jason Dean, thank you. Your solution worked great! I tried the patches and they didn't work for me. But, this simple Rule did!

Anybody’s picture

Status: Needs review » Needs work

Needs reroll against latest 7.x-1.x-dev! Sorry.