0

I'd like to setup two content types in a parent -> child relationship.

Node A of content type "parent" has custom field:

  1. (text) Description

Node B of content type "child" has custom fields:

  1. (text) Description dynamically inherited (referenced) from parent node.
  2. (reference) to parent node

When the description of Node A changes, Node B automatically reflects this change.

I'd like to achieve this entirely on the backend, no UI or forms needed.

I will be creating the child nodes programmatically.

Topics I've found have danced around what I'm trying to do but lack the details I need to execute it. inheriting fields from a base content type

I have experience with Drupal 6 & 7 but am new to Drupal 8's entity apis.

A detailed explanation or example of how to achieve this would be greatly appreciated.

Thanks

One possible solution:

Store the parent node's updated date in the child node. Upon loading the child node, lookup the parent node's updated date, if it differs, copy the parent's field value to the child.

The parent node will not be updated frequently.

  • Is there going to be an entity_reference that is shared between the parent and child? Single value or multi-value? Does the Child content type need to have the description as a field, or can the display look up the value from the parent and just display that? – Kevin Nowaczyk Jan 5 '18 at 23:10
  • Yes, there will be an entity_reference between the two (i'll update the question to reflect this). The child node will need to be able to provide the data when the node is loaded. I suppose a hook_alter type setup would work. So when the child node is loaded, the parent node is looked up and the field value returned. – tseven Jan 6 '18 at 0:04
0

The way I would tackle this is to create a new field type called “parent_value”.Design it in such a way that it doesn’t actually store any data in the database but instead uses the existing object’s entity reference value to look up the value from the parent. This way you don’t have to rely on any sort of Dripal trigger to alter the value in the child, which will ensure that if values are updated programically, they will still match.

There is also the added advantage that creating a custom field will teach you everything you need to know about Drupal 8 plug-ins.

  • If I understand you correctly your suggesting that the custom field's value is a db/cache lookup of the parent's matching field? – tseven Jan 6 '18 at 0:58
  • Yeah. I’ve never done it but certainly there would be a way to create an entity field for which you provide a name of an Entity reference from the child and the field name of the parent for which you would like the value. Instead of grabbing a database value it would use the entity manager to grab the parent entity and return the parents value. I am by no means an expert in Drupal, but this certainly seems feasible. – Kevin Nowaczyk Jan 6 '18 at 1:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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