- Issue created by @nickdjm
- 🇦🇺Australia NicolasH
Yes please, this makes perfect sense. I wouldn't use UI Patterns at all if it wouldn't have the option to use it in render arrays and twig files apart from the UI options...as this allows to use the same patterns in more complex scenarios as well and still keeps it consistent.
I actually thought this was already built in here and was looking for documentation on it.
I've tested the patch on only one scenario for now and it works well. Will update if I find anything with more scenarios.
- Status changed to Postponed
over 1 year ago 3:44pm 24 August 2023 - 🇫🇷France pdureau Paris
The current pattern() function already allow to inject a mix of fields ("slots") and settings ("props") in its second parameter :
new TwigFunction('pattern', [ $this, 'renderPattern', ]), ]; } public function renderPattern($id, array $fields = [], $variant = "") { return [ '#type' => 'pattern', '#id' => $id, '#fields' => $fields, '#variant' => $variant, ]; }
We don't need to use the specific #settings property because at the end the #fields and #settings are flattened and merged before being rendered with the template : https://git.drupalcode.org/project/ui_patterns/-/blob/8.x-1.x/src/Elemen...
For example, in https://git.drupalcode.org/project/ui_suite_dsfr/-/blob/1.0.x/templates/...
pattern('modal', { modal_id: modal_id, title: modal_title, ... })
Where modal_id is a setting ("prop") and title is a field ("slot") : https://git.drupalcode.org/project/ui_suite_dsfr/-/blob/1.0.x/templates/...
Are you OK with that?
- Status changed to Closed: outdated
about 1 year ago 12:29pm 18 October 2023 - 🇦🇺Australia NicolasH
Right, I see, thanks for that. I stumbled across this by accident myself when putting in some settings like any other field and it just worked.
My only comment would be that the twig function reads a bit nicer with the settings in their own array, especially is there are a large number of fields and settings. It also makes more sense when using it both in the UI as well as twig.
Also, I've been using a separate
#settings
array key in render arrays and it works fine. I can't remember whether I just tried it out or whether it's somewhere in the documentation. After reading your comment I tried to just move them into the#fields
array and yes, works just the same. Again, the readability will have me probably leaving them separate.$build[['register_link'] = [ '#type' => 'pattern', '#id' => 'dialog_button', '#fields' => [ 'url' => $create_account_url, 'text' => t('No account? Sign up'), ], '#settings' => [ 'link_type' => 'button_primary', 'size' => 'm', 'icon_classes' => 'fa-solid fa-user-plus', ], ];