0

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()?

3
  • You can use setConstraints() to set an entire array, but then you need to apply the correct array structure, where the options are the array values. addConstraint() is more user friendly and it should work in your case as well.
    – 4uk4
    Commented Oct 12, 2022 at 6:30
  • @4uk4 Yeah, I was using the wrong method, as you point out. I think your comment is the answer so please repost as an answer so that I can accept. Commented Oct 12, 2022 at 13:51
  • 1
    If you still wanted to use setConstraint() here is a working example git.drupalcode.org/sandbox/Bhanu951-3103712/-/blob/8.x-dev/… Commented Oct 12, 2022 at 17:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.