Adding a constraint to a BaseField is broken if the basefield is overwritten

Created on 18 January 2021, over 3 years ago
Updated 11 April 2024, about 2 months ago

Problem/Motivation

If I want to add a constraint to a node title field I assume this needs to be done in hook_entity_base_field_info_alter(). But this works only if the title field is not overwritten. E.g. renamed from "title" to "name". If an overwrite is present the title field is considered a bundle field and the constraint needs to be added in hook_entity_bundle_field_info_alter().

This breaks installation as during install the overwrite might not be present and then the "title" field is not available in hook_entity_bundle_field_info_alter().

If we add the constraint in hook_entity_base_field_info_alter() site installation works but during runtime the constraint will not run as the title field is now a BaseFieldOverride and bundle specific.

As a workaround you need to implement both hooks and check the fields array for existence of the field carefully before adding the constraint. You cannot assume that the title field is always present. This is especially bad if a base field is overwritten later through the UI as the user might not be aware that this can break the hook implementation.

If this behaviour is intended it should be highlighted on the api docs.

Steps to reproduce

  • Add constraint to a node title field that has been renamed from "title" to "name" in hook_entity_bundle_field_info_alter()
  • A site-install will fail as the title field is not yet present in this hook.
  • Add constraint to overwritten title field in hook_entity_base_field_info_alter()
  • Nothing happens even if the constraint is violated. It is simply ignored.
πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
EntityΒ  β†’

Last updated 1 day ago

Created by

πŸ‡©πŸ‡ͺGermany marcusx

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States Kristen Pol Santa Cruz, CA, USA

    Thanks for reporting this issue. We rely on issue reports like this one to resolve bugs and improve Drupal core.

    As part of the Bug Smash Initiative, we are triaging issues that are marked "Postponed (maintainer needs more info)". This issue was marked "Postponed (maintainer needs more info)" almost a year ago and there has been no activity since that time.

    Since we need more information to move forward with this issue, I am keeping the status at Postponed (maintainer needs more info). If we don't receive additional information to help with the issue, it may be closed after three months.

    Thanks!

  • Status changed to Needs work 12 months ago
  • πŸ‡ΊπŸ‡¦Ukraine gilmord πŸ‡ΊπŸ‡¦Ukraine

    Does comment #4 resolve this?

    Hi @larowlan
    Number 4 seems to work - but this is a hack, you need to keep in mind to implement hook_entity_bundle_field_info every time you override a field with a validation constraint and re-add the constraints to the override.

    For sure this should be automated, otherwise, developers will get into debugging hell trying to find why some constraints suddenly stop working at all.

  • Status changed to Active about 2 months ago
  • πŸ‡ΊπŸ‡ΈUnited States m.stenta

    We just encountered this in farmOS, where we have a base field on our entity type, and an optional module that wants to add both a constraint and override the label of the field on a specific bundle.

    The workaround of using hook_entity_bundle_field_info() instead of hook_entity_field_info_alter() to add the constraint fixed the issue.

    See: https://github.com/farmOS/farmOS/blob/91a4cdc4e7055c12697edd169ff0b11150...

    But I agree this workaround shouldn't be necessary, so this is still a valid bug in my opinion.

Production build 0.69.0 2024