I have an entity type animal with the following bundles:

  • cow
  • pig
  • sparrow
  • anteater

In the Animal.php entity type definition, I create some base fields, one of which is an entity reference to Habitat.

  ->setLabel(t('Habitat ID'))
  ->setSetting('target_type', 'habitat')
  ->setDisplayConfigurable('form', FALSE)
  ->setDisplayConfigurable('view', TRUE);

I want to require this entity reference for cows and pigs, but I don't want it to be required for sparrows and anteaters.

So, I can't use ->setRequired() in BaseFieldDefinition::create() because that will apply to all the bundles.

What's the simplest way to require a field created by BaseFieldDefinition::create() in some bundles but not others?


1 Answer 1


I haven't done it, but I believe you can override ContentEntityBase::bundleFieldDefinitions(), returning the fields based on the bundle type.

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.