0

I have 3 custom modules that provide custom entities with all fields defined as base fields.

Contact module - provides a contact entity(it could be one of the type: a person or email or phone etc) that has a store entity reference field.

Store module - provides a store entity that has a region entity reference field.

Region module - provides geographical region entity that has a contact entity reference field.

So

  1. Contact depends on Store
  2. Store depends on Region
  3. Region depends on Contact

Each definition would look like this:

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  ...
   $fields['store'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Store'))
    ->setDescription(t('Select the store the contact is part of.'))
    ...
    ->setSetting('target_type', 'custom_store')
    ...
}

As these modules provide more than just entity (and custom field API plugins), We can't combine them together.

when trying to install the modules, getting an unknown entity target_type error.

1

Since the dependency is purely on the reference field, we could add the target_type later. For example, setting the target_type at the bundleFieldDefinition wouldn't throw the install's error and set the right target_type.

So the baseFieldDefinitions would be

public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
  ...
  $fields['store'] = BaseFieldDefinition::create('entity_reference')
    ->setLabel(t('Store'))
    ->setDescription(t('Select the store the contact is part of.'))
    ...
}

And the bundleFieldDefinitions would be

public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) {
  $fields = parent::bundleFieldDefinitions($entity_type, $bundle, $base_field_definitions);
  $fields['store'] = clone $base_field_definitions['store'];
  $fields['store']->setSetting('target_type', 'custom_store')
  ...
  return $fields;
}

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.