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.
BaseFieldDefinition::create('entity_reference')
->setLabel(t('Habitat ID'))
->setCardinality(1)
->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?