I have two content types: business listing, product listing. "Product listing" has an Entity Reference field. This field points to the nodes of "business listing". I want to allow user to choose from his own "business listing" nodes (during adding a "product listing" node). I have created a view to list the nodes of "business listing" type and have Added a Entity Reference display to the view. Then have selected the view Under "Entity selection". Now, I have a problem in adding proper contextual filter. I have added a contextual filter of "Content: Author Uid" and selected "provide default value".

a) if I select "USER ID from logged in user" as default value:

This solution will break if an admin is editing the node. The field will then show only nodes the current user (admin) has created.

b) if I select {"User ID from URL" and "Also look for a node and use the node author"}: This solution will break if an user is adding a new node. The field will not show any nodes!

How can I achieve this?

  • I want to allow user to choose from his own "business listing" nodes. please explain why a) is a problem?
    – No Sssweat
    Feb 28, 2016 at 23:31
  • hi, if an admin is editing the nodes of other users. the field will show only nodes the current user (Admin) has created (admin is logged user) Feb 28, 2016 at 23:34

3 Answers 3


For type: select PHP Code

enter image description here

and put this code in there.

$url = $_SERVER['HTTP_HOST'] . request_uri();
$getnid = explode("/", $url);
$var = $getnid[2];

if (is_numeric($var)) { 

  $node = node_load($var);
  return $node->uid;

} else { 

  global $user;
  return $user->uid;


Explanation of code

Line 1 gets the current page url

Line 2 implements the explode php function

Line 3 grabs the 2nd / from the url

Then we use an if statement to check if line 3 is a number or not:

  • If it is a number then it has to be the node/2/edit form. The 2 is the nid, which we then use to grab the author (uid).
  • Else, if it's not a number, then we know that it is the node/add form. So since it doesn't have a number in the url, we use the logged in user uid instead.
  • 1
    Is there an advantage to getting the argument that way instead of using arg(#)? Feb 29, 2016 at 5:20

No need to write any code for this. Use reference_option_limit module properly in your content types & when you create view and provide default value 'User ID from URL' - Please mark the checkbox 'Also look for a node and use the node author' to true.

Enjoy !!!


You could add the author uid to the url for creation and editing.

node/add/contenttype/123 and node/#/edit/123 where 123 is the current user during creation and is the author during edit.

Make your entity reference view pick the raw value from url.

  • By using tokens or replacement patterns for the create node and edit node links. The entity reference view raw value should be 4 I think. How does a user create a new node at the moment? Feb 28, 2016 at 23:52
  • I have tried to use tokens by pathauto. Feb 28, 2016 at 23:56
  • now, my url is : example.com/node/add Feb 28, 2016 at 23:57
  • You'd need to create a new place for node add links.. In a block or menu. The links would be node/add/article/123 for example. Feb 29, 2016 at 0:01
  • Although I can see this method working, it's an ugly way to solve the problem.
    – No Sssweat
    Feb 29, 2016 at 3:07

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.