1

I'm building a custom module to handle two custom entities which extend ContentEntityBase: Organization and Contact. Organization can have multiple contacts, while contact can belong to one organization.

I have defined baseFieldDefinitions method for each entity:

Organization:

$fields['contacts'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Contact'))
      ->setDescription(t('Name of the contact'))
      ->setSetting('target_type', 'mymodule_contact')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
        'weight' => -3,
      ])
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => -3,
      ])
      ->setRequired(true)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

Contact:

$fields['organization'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Organization'))
      ->setDescription(t('Name of the organization'))
      ->setSetting('target_type', 'mymodule_organization')
      ->setDisplayOptions('view', [
        'label' => 'above',
        'type' => 'entity_reference_label',
        'weight' => -3,
      ])
      ->setDisplayOptions('form', [
        'type' => 'entity_reference_autocomplete',
        'weight' => -3,
      ])
      ->setRequired(true)
      ->setDisplayConfigurable('form', TRUE)
      ->setDisplayConfigurable('view', TRUE);

When I assign contacts to organization via Organization entity form, the Contact entity reference records are stored in mymodule_organization__contacts table. However, when I'm adding a new contact via Contact entity form, the Organization relationship is not recorded. mymodule_contacts will assign organization value to the selected organization_id, however, the record is not added to mymodule_organization__contacts table.

ChatGPT and other resources are suggesting to write a hook or to write custom save() method inside Contact entity. However, I find it hard to believe that Drupal didn't foresee handling one-to-many, belongs-to-one without hooks/custom save methods. There's got to be a better way, right?

2

1 Answer 1

0

I ended up writing the custom save method. Inside Contact entity class:

  public function save()
  {
    // save contact entity (this will generate id for the contact record)
    parent::save();

    // load Organization entity based on form selection
    $organization = \Drupal::entityTypeManager()
      ->getStorage('mymodule_organization')
      ->load($this->get('organization')->target_id);

    // save contact entity to organization as entity reference
    $organization->contacts[] =  ['target_id' => $this->id()];;
    $organization->save();

  }
1
  • 1
    That's not all you need to maintain a two-way reference. But most times you don't need to save references both ways, you can get the data for the missing direction via an entity query.
    – 4uk4
    Commented May 16 at 6:43

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.