Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
The creation of new entities is done in a method used as an #element_validate callback.
The widget API in D8 has the massageFormValues() method, which would be much more adapted to the task (also, results in less stuff being serialized in the $form)
Comments
Comment #1
yched CreditAttribution: yched commentedSide note: this actually become a requirement once we start actually running validate() and make use of our constraints:
EntityReferenceItem::getPropertyDefinitions() specifies that 'target_id' has to be a positive int.
But the autocomplete widget generates items with 'target_id' = 'the label of the auto_create entity', thus fails validation.
Comment #2
yched CreditAttribution: yched commentedBumping down. The issue mentioned in #1 is actually also encountered by the taxo autocomplete widget, and is triggered by something else. I opened #2012662: Constraints on 'target_id' / 'tid' properties break autocomplete if applied for this.
Still, the refactoring mentioned in the OP would make very much sense :-)
Comment #3
amateescu CreditAttribution: amateescu commentedI looked a bit into doing this at the post-DrupalcampGhent sprint but there are a few problems:
elementValidate()
method:I'm not sure the semantics of
massageFormValues()
is ok with that?elementValidate()
is only called once during a successful form submission, whilemassageFormValues()
is called three timesComment #4
amateescu CreditAttribution: amateescu commentedThis was fixed in #1959806: Provide a generic 'entity_autocomplete' Form API element.
Comment #5
yched CreditAttribution: yched commentedYay :-)