Drupal 8

Here is the scenario

I have a Company > Department > Employee… in that hierarchy.

Companies are already created previously.

On the Department node add form, there is an entity reference field to reference a Company. On that same Department node form, there is another Entity Reference field that leverages Inline Entity Form to add + reference Employee nodes on the fly. These Employee nodes also have a Company entity reference field.

The goal is to pass the Company field value from the Department node to the referenced Employee nodes when the Department form is saved.

I figure this may be accomplished with the Business rules module, and/or a hook form alter. Tried both without any traction. Any tips?

  • Is it neccessary to save the Company info into Employee entities or would just showing what Company the Emloyee belongs to on their page would be enough?
    – prkos
    Feb 14, 2021 at 20:04
  • Unfortunately, it is necessary to save the Company info directly into Employee entities. There are some complex display and view filter outputs that require it.
    – Yuckle
    Feb 14, 2021 at 21:43
  • Example... Employees can be created outside of Departments. And I need an Employee view output to be filterable by Company. The filter would then need to leverage the Company field in both the Department and Employee content type (instead of just the Employee content type). This would complicate things long term.
    – Yuckle
    Feb 14, 2021 at 21:50
  • I expected the necessity to come from something else, like specific company policy, or site performance. You can use Views and Content View modes to show referenced information, without it being saved on the entity item that is related to it.
    – prkos
    Feb 14, 2021 at 23:01
  • I hear you... combining a referenced field and non-referenced field into the same views filter is the ultimate challenge though. Any ideas here? For long term scalability, my above request still seems like the ideal solution.
    – Yuckle
    Feb 15, 2021 at 17:44

1 Answer 1


One option is to use hook_ENTITY_TYPE_update() and hook_ENTITY_TYPE_insert().

// Top of .module
use Drupal\node\NodeInterface;

function MYMODULE_node_update(NodeInterface $node) {

function MYMODULE_node_insert(NodeInterface $node) {

function _MYMODULE_set_department_employee_company(NodeInterface $node) {
  if ($node->bundle() == 'department') {
    $dept_company_id = $node->DEPARTMENT_COMPANY_FIELD->target_id;
    foreach ($node->DEPARTMENT_EMPLOYEE_FIELD->referencedEntities() as $employee) {
      // Only update the employee company if it's different.
      if ($employee->EMPLOYEE_COMPANY_FIELD->target_id !== $dept_company_id) {
        $employee->EMPLOYEE_COMPANY_FIELD->target_id = $dept_company_id;
  • I'm not sure if you'll run into any issues when IEF saves the employees - I'm not sure if IEF saves entities before or after the parent node is saved.
    – sonfd
    Feb 14, 2021 at 22:57
  • @sonfid - Great solution. Works flawlessly. Thanks again!
    – Yuckle
    Feb 15, 2021 at 18:25
  • Note: I'd always wrap field work with $entity->hasField(MY_FIELD) before trying to access them or set their values. I didn't here just to keep the code focused on the issue at hand.
    – sonfd
    Feb 15, 2021 at 18:44
  • @sonfid - So you'd wrap the "foreach" statement above with if($node->hasField(DEPARTMENT_EMPLOYEE_FIELD)){} that sound right?
    – Yuckle
    Feb 16, 2021 at 21:06
  • I'd check $node before setting $dept_company_id and $employee before checkout the company field on it.
    – sonfd
    Feb 16, 2021 at 21:30

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.