What I want to try and achieve.

An authenticated user creates a node of type 'listing' (this node obviously belongs to this user via his uid). On this node will be a link to create a new node of a different content type 'booking-request' (this link the author can't control).

Now an anonymous user can view this created 'listing' node and then they can click on the link and create a 'booking-request' node. So obviously the 'booking-request' node will belong to the anonymous user.

My question is. How can I assign this 'booking-request' node to the author of the 'listing' node after the anonymous user saves it?

Can I do this with a module or will this need custom php or both and how will I do this?

2 Answers 2


You can use Entity Reference Prepopulate.

Allow prepopulating any Entity reference field via URL

In your booking-request set up an entity reference field pointing to user entities. Then, create a link in the listing node to booking-request node add form with the right parameter to prepopulate that entity reference field.

See README in the module:

In order to pre-populate an entity reference field you have to supply the parameter in the URL.

The structure is node/add/article?[field_ref]=[id]

Where [field_ref] is the name of the entity reference field and [id] is the id of the entity being referenced.

Examples: node/add/article?field_foo=1 node/add/page?field_bar=1,2,3


You can do this in following way :

  1. You can do this using Rules, by checking the condition if author is anonymous of newly added content then add action, select Set data value. Your target should be node:author. Assign the value you want.
  2. Using hook_node_insert, add condition inside your hook :

    if ($node->type == 'your_type' && !$node->uid) { $node->uid = 1; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.