8 – How do I create linked nodes without user intervention?


Suppose I have a cooking website where users can create Recipe nodes for Food nodes. For example, you might have a Cake node, and users should see an Add Recipe button which takes them node/add/recipe except that the newly created Recipe node will have a reference to the parent Food node.

I’ve added a Content type Entity Reference to to the Recipe node, which works fine. For programmatically generating the link I’m a bit lost though. I initially looked at using the Rules but it’s still in alpha, and from the description it looks like I’d probably want to use the Rules Link module as well, but it doesn’t have a version for 8.x at all.

Then I started looking at hook_ENTITY_TYPE_create, which is my current best guess for how to achieve the desired behaviour, but that still leaves me with some questions about what the best approach is;

  1. How / Where do I capture the node reference when all I’m given is the EntityInterface type?
  2. What is a mechanism for passing information about the reference/parent node to hook_ENTITY_TYPE_create so that I can even know which reference to store?
  3. How do I create links or buttons on the parent Food node that cause such information to be passed on to the hook?
  4. Lastly, is this even the best approach, or am I missing a way that doesn’t involve writing code?

I don’t think I need any hand-holding or code, but I’d be very grateful for some pointers in the right direction for each of the above points from a veteran.