Hello!
I'm wondering how to best go about adding a new property to *all* yamlform elements (i.e General Settings, Form Display etc), if I have a certain handler attached to the form.
I have the field appearing by using hook_form_alter and seeing if its an instance of YamlFormUiElementEditForm and adding my new field to the &$form array, so that's all good, my issue is saving the data. It would be great if I could add this new property to the existing custom properties field and in effect just have my custom element properties to be a nice gui into that custom properties field. That way I can hide the custom properties field in the front end and only allow people to give certain custom properties.
Is there a hook i can use where I can simply do something like $element->customProperties += $element->get('my_new_property')?
Thanks.
*Edit* a little bit of context. I have a handler that will hit an API endpoint, the problem is that the endpoint stays the same, but the fields I need to post through will have differnent names for each separate yamlform and I need a nice user friendly way of specifying that API field name.
Comments
Comment #2
siliconandincense CreditAttribution: siliconandincense commentedComment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedThat context helps a lot and explains exactly why you need to be able alter all element's properties. I am hoping a well crafted hook_form_FORM_ID_alter() might do the trick.
Comment #4
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedI have thought about how one would go about properly adding custom properties to an element and it is going to require some API changes and new hooks. This task will get done, I just need to plan out the best way to do it.
For now you are going to have manually enter the custom properties as YAML.
Comment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedAnother use case for this feature would be to add custom validation properties/rules to an element.
Comment #6
fenstratMoving to Webform queue, see #2827845: [roadmap] YAML Form 8.x-1.x to Webform 8.x-5.x.
Comment #7
siliconandincense CreditAttribution: siliconandincense commentedSorry for the delay.
Thanks for this!
In the end I ended up doing this (a quick abbreviated version):
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer commented@AudioUnderGround Yep your code makes sense to me. I think your custom property may also be appearing under the 'Custom properties' YAML once the element is submitted.
I have been actively refactoring some of the UI related APIs which will make adding custom properties even easier. I am going to setup a test form that demonstrates the easiest and most maintainable way to add custom properties.
Comment #9
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedAll work for this ticket is going to occur via #2833236: Custom element properties in the YAML Form module's code base.
Comment #10
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedSo below is the test I have setup that can be used as an example for defining custom element properties.
http://cgit.drupalcode.org/webform/tree/tests/modules/webform_test_custo...
While building this test/example it became really obvious that I am going to need to add hook_webform_element_alter() and hook_webform_element_ELEMENT_TYPE_alter() hooks that work similar to hook_field_widget_form_alter().
@AudioUnderGround I hope this example helps.
Comment #11
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedHere is ticket for #2833409: Provide webform element alter hooks.
This example will also be updated to use hook_webform_element_alter().