Master Content Type:
Has a field called "Assigned User" that is an entity reference to User. When creating nodes of this content type this field gets assigned to certain users in our system.

Child Content Type:
Has a field called "Related node" that is an entity reference to nodes of type Master.

When a user creates nodes of type Child, he or she selects the "Related node" drop-down box, the user should only see Master nodes that have "assigned user" set to that user's username, and not nodes assigned to other users. How do I do this?

Comments

nevets’s picture

If the user is creating the node, then the author is the person who created the node.

If the "assigned user" ever going to be not the author of the node?

MrSnrub’s picture

The "assigned" user does not create nodes of type Master. That is done by a completely different set of users.

The "assigned" user will always be the author the nodes of type Child.

nevets’s picture

You can use a view to populate the entity reference field to content of type master.

So you could create a view that uses a relation on the assigned field that references the user and then add a filter on user:current that uses that relation.

MrSnrub’s picture

I'm sorry, I don't quite understand. Could you please explain in more detail?

I go to Structure --> Views --> Add new view.
I say to show Content of type Master.
I click "Continue and Edit."
Under "Displays" I click Add --> Entity Reference.
I click on the Entity Reference Settings link and click on "Content: Title," then click on "Apply (this display)."
I expand the "Advanced" tab and go to Relationships --> Add.

And this is where I'm stumped. How do I add the "user: current" filter?

MrSnrub’s picture

Never mind. It took quite a while, but I figured it out. Here is what I did:

  1. Go to Structure --> Views --> Add new view.
  2. Set the View name to "Show Master Nodes Assigned to Current User." Show Content of type Master sorted by Newest first. Create a page with display format of Table of fields. Click "Continue & edit."
  3. Expand the "Advanced" tab. Next to "Relationships," click Add.
  4. Check the box next to "Entity Reference: Referenced Entity - A bridge to the User entity that is referenced via field_assigned_user." Click "Apply (all displays)."
  5. Click the checkbox next to "Require this relationship." Click "Apply (all displays)."
  6. Next to "Contextual Filters," click Add.
  7. Click the checkbox next to "User: Uid - The user ID." Click "Apply (all displays)."
  8. Under "When the Filter Value is Not in the URL," click "Provide default value." Under "Type," select "User ID from logged in user." Click "Apply (All displays)."
  9. In the main view editing screen (under the Displays heading), click Add, then click Entity Reference. Save the view.
  10. Now go to Structure --> Content Types and edit the "Related Master Node" field of the Child content type.
  11. Set the Target Type to "Node," the Entity Selection to "Views: Filter by an entity reference view," and View used to select the entities to "show_master_nodes_assigned_to_current_user - Entity Reference." Click "Save settings" and you're done.