I'm refactoring a node content type into a custom entity.
On this entity, I add a datetime field:
$fields['target_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('Target date'))
->setCardinality(1)
->setRequired(TRUE)
->setConstraints(['DateTimeMidnightOnly' => DateTimeMidnightOnlyValidator::class])
->setSetting('datetime_type', 'datetime')
->setDisplayConfigurable('form', TRUE)
->setDisplayOptions('form', [
'weight' => 10,
])
->setDisplayConfigurable('view', TRUE);
However, when I attempt to save a new entity of this entity type, I'm getting the following error:
Drupal\Core\Entity\EntityStorageException: No default option is
configured for constraint
"Drupal\mymodule\Plugin\Validation\Constraint\DateTimeMidnightOnly". in
Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 811 of
/app/web/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).
I also tried this:
->setConstraints(['DateTimeMidnightOnly'])
Which gives the error:
Drupal\Core\Entity\EntityStorageException: The "0" plugin does not
exist. Valid plugin IDs for Drupal\Core\Validation\ConstraintManager
are: Callback, Blank, NotBlank, Email, CKEditor5Element,
CKEditor5ToolbarItemDependencyConstraint, StyleSensibleElement,
CKEditor5MediaAndFilterSettingsInSync, SourceEditingRedundantTags,
CKEditor5EnabledConfigurablePlugins,
CKEditor5FundamentalCompatibility,
SourceEditingPreventSelfXssConstraint, UniqueLabelInList,
CKEditor5ToolbarItemConditionsMet, CKEditor5ToolbarItem, CommentName,
DateTimeFormat, ValidDynamicReference, FileValidation....
I'm clearly calling setConstraints()
wrong, because this constraint worked when I added it on my content type using hook_entity_bundle_field_info_alter()
:
function mymodule_entity_bundle_field_info_alter(array &$fields, EntityTypeInterface $entity_type, string $bundle): void {
$entity_type = $entity_type->id();
if ($entity_type === 'node') {
if (isset($fields['field_datetime_review_target'])) {
$fields['field_datetime_review_target']->addConstraint('DateTimeMidnightOnly');
}
}
}
What's the right way to set ->setConstraints()
to add a custom constraint plugin? Or, is this actually the wrong way and I should always use hook_entity_bundle_field_info_alter()
?