Compatibility between SDC and the Form API

Created on 17 December 2024, 4 days ago

Problem/Motivation

SDC doesn't play well with the Form API:

  • we can put a full form into a component slot, but we can't put a form element of a form defined outside the component
  • we can't define form element, directly usable with the Form API, with SDC. So we can't easily implement some design systems components like https://getbootstrap.com/docs/5.3/forms/checks-radios/#switches

This ticket is focused on the first issue, but let's keep the second one in mind.

Restriction of a chat between @pminf @mmbk & @pdureau: https://drupal.slack.com/archives/C4EDNHFGS/p1734453836627559

While processing a form the FormBuilder handles only children, returned by Element::children($element) Render elements located in #slots are not processed, so they won't get the meta data that are necessary to process the input field, I hacked around that problem by adding the select field a top-level element of the component and added a preRenderCallback to the SDC that is moving the select field into the slot.

This may be the same issue as the one with table render element.

Proposed resolution

Is it possible to merge #slots and #props into Element::children()

For example:
<code>#type: component
#component: 'provider:foo'
#slots:
  slot_1: {}
  slot_2: {}
#props:
  prop_1: ''
  prop_2: ''

Would become:

 #type: component
#component: 'provider:foo'
slot_1: {}
slot_2: {}
prop_1: ''
prop_2: ''

There is no risk in merging slots & props because they already share the same namespace as Twig template variables.

But it may be not as easy and safe as adapting the form API to this special case. And fixing this at the form API level can also help us to fix the similar cases (like table for example)

API changes

Maybe. Let's be careful.

📌 Task
Status

Active

Version

11.1 🔥

Component

single-directory components

Created by

🇫🇷France pdureau Paris

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

Comments & Activities

Production build 0.71.5 2024