Problem/Motivation
In UI Patterns 2, we didn't care so much about required props because it was managed by the Form API:
- if a prop is required, we add the
#requiredrender property - ComponentForm can be submitted without filling a value
- So it is not possible to render a component without required props filled
In Display Builder, we can render component without subimiting ComponentForm:
- on preview panel
- on builder panel
- also on final dsipaly rendering if the component was only dropped without any change in ContextualForm
So we have this fatal error:
Twig\Error\RuntimeError: An exception has been thrown during the rendering of a template The property xxx is required."
Proposed resolution
Add default values to required props.
There is 2 places where we can do this:
ComponentLibraryPanel: this is the best place in my himble opinion, because the logic is executed only once by buildign session and can be easily cachedInstance::attachToRootandInstance::attachToSlot, so executed at each attachment
The logic, if the prop is required
- Set the
defaultvalue if exist - Set the first
examplesvalue if exist
We can go further, but it would be come complicated.
Other tasks
Do we need to add the rule in sdc_devel: "each required prop must have a default value" ?
Issue fork display_builder-3549348
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #3
pdureau commentedThe MR draft is still in an early, naive, state:
But it is enough for you to test the idea with proejcts heavily relying on required props in components, like the Oliveor theme of Drupal CMS.
Comment #4
pdureau commentedTry again
Comment #5
pdureau commentedComment #6
pdureau commentedComment #7
pdureau commentedTested by Dang Tran (before the logic has been moved to the helper), it was OK
The helper asked this morning has been done but:
Follow-up #3549761: Fatal: Default value for props, on update
Comment #8
mogtofu33 commentedWill fix the helper and add unit test.
Comment #9
mogtofu33 commented