Drupal - how do I set a content entity field to be required

You need to use the ->setRequired(TRUE) option on the field. IE

$fields['source_id'] = BaseFieldDefinition::create('entity_reference')
      ->setLabel(t('Thingy'))
      ->setDescription(t('Pick a thingy'))
      ->setSetting('target_type','thingy')
      ->setRequired(TRUE)
      ->setDisplayOptions('form', array(
        'type' => 'options_select',
        'weight' => 5,
      ))
      ->setDisplayConfigurable('form', TRUE);

You will also need to uninstall and then reinstall your module to have the change reflected in the database before this will work.

Tags:

Entities

8