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

AudioUnderGround created an issue. See original summary.

siliconandincense’s picture

Issue summary: View changes
jrockowitz’s picture

That 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.

jrockowitz’s picture

Category: Support request » Task
Priority: Normal » Major

I 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.

jrockowitz’s picture

Another use case for this feature would be to add custom validation properties/rules to an element.

fenstrat’s picture

Project: YAML Form » Webform
Version: 8.x-1.0-beta22 » 8.x-5.x-dev
siliconandincense’s picture

Sorry for the delay.

Thanks for this!

In the end I ended up doing this (a quick abbreviated version):

hook_form_alter() { 
 // if this form is YamlFormUiElementEditForm or YamlFormUiElementAddForm
 // get the custom element: $form['properties']['custom']['custom']['#value']

$form['new_fieldset'] = [
      '#type' => 'fieldset',
      '#title' => t('New Fieldset'),
    ];
    $form['new_fieldset']['new_element'] = [
      '#type' => 'textfield',
      '#title' => t('New Element'),
      '#default_value' => isset($custom['new_element']) ? $custom['new_element'] : '',
    ];

    array_unshift($form['#submit'], '_ammend_yaml_custom');
}

function_ammend_yaml_custom(&$form, FormStateInterface $form_state)
{
  // Get the element name
  // Get the new element value
  // set the yaml form custom element properties to 
  $form_state
    ->getFormObject()
    ->getYamlForm()
    ->setElementProperties(
      $key,
      $new_element
      ),
      $form_state->getValue('parent_key')
    );
}
jrockowitz’s picture

@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.

jrockowitz’s picture

All work for this ticket is going to occur via #2833236: Custom element properties in the YAML Form module's code base.

jrockowitz’s picture

Status: Active » Fixed

So 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.

jrockowitz’s picture

Here is ticket for #2833409: Provide webform element alter hooks.

This example will also be updated to use hook_webform_element_alter().

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.