Default entity reference selection plugin doesn't validate newly created entities leading to invalid entities

Created on 24 July 2018, over 6 years ago
Updated 24 March 2025, 15 days ago

Problem/Motivation

The default entity reference selection plugin (Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity) doesn't validate newly created entities which could lead into potential data integrity problems. Drupal doesn't ship with broken configurations by default, but it is very easy for users to configure in a way that leads into data integrity problems. For example, adding additional required fields besides label/title for auto creatable entity type or bundle would automatically lead into data integrity problems.

Proposed resolution

  1. Add UI restrictions to prevent users from creating broken configurations on their sites
  2. Validate auto created entities in Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection::createNewEntity

Steps to reproduce

  1. Install standard profile
  2. Add a required field to the tags taxonomy
  3. Ensure the tags field on Article autocreates entities
  4. Create a new article with a tag that doesn't exist.
  5. Verify the term is created with the new required field empty.

Remaining tasks

-

User interface changes

-

API changes

This might create some API changes since we would do more validation. However, this only happens when user has already misconfigured their site.

Data model changes

None, but this will add stricter validation validation for new data being created through the default entity reference selection plugin.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component

entity system

Created by

🇫🇮Finland lauriii Finland

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

Merge Requests

Comments & Activities

Not all content is available!

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

  • First commit to issue fork.
  • Pipeline finished with Failed
    15 days ago
    Total: 138s
    #455853
  • 🇮🇳India KumudB Ahmedabad

    Steps to Reproduce:

    1. Install a standard Drupal profile.
    2. Add a required field to the Tags taxonomy.
    3. Ensure that the Tags field on the Article content type is configured to auto-create new terms.
    4. Create a new Article with a tag that does not exist.
    5. The new term is created with the required field empty, leading to data inconsistency.

    Here I have attached After fixing Screen shot

    After fixing
    ScreenRecordingofErrorthrowing

  • Pipeline finished with Failed
    15 days ago
    Total: 5771s
    #455880
  • 🇬🇧United Kingdom MrDaleSmith

    Not 100% sure about the message being set - see comments on MR.

    Possibly unrelated to this issue, but this change would effectively make the "create term if it doesn't exist" setting impossible to use on any term vocabulary that had required fields: you would never be able to create the terms as the validation would always fail but the edit form has no ability to let you set the field values on the new term.

    My personal preference here would be the current behaviour - stub taxonomy created that requires future updating - so that the setting on the field actually has a function. I can understand that not everybody would, however: perhaps we need some kind of validation on the field settings form that advises terms with required fields can't use this setting (if this change is ti progress)? Or at least a warning in the help text?

  • Pipeline finished with Running
    13 days ago
    Total: 1678s
    #457742
Production build 0.71.5 2024