Follow-up to #2809971: composable forms and Getting NYU onto YAML Form

Problem/Motivation

Site builders like be able to 'extend' a base form or combine multiple forms into a composite form.

Proposed resolution

Provide mechanism to build composite and/or base forms

Remaining tasks

User interface changes

API changes

Data model changes

Comments

jrockowitz created an issue. See original summary.

jrockowitz’s picture

Right now, writing a custom composite element is best way to get a group of reusable elements.

Below are the two plugins and templates used to implement an Address element.

http://cgit.drupalcode.org/yamlform/tree/src/Element/YamlFormAddress.php
http://cgit.drupalcode.org/yamlform/tree/src/Plugin/YamlFormElement/Yaml...
http://cgit.drupalcode.org/yamlform/tree/templates/yamlform-composite-ad...

fenstrat’s picture

Project: YAML Form » Webform
Version: 8.x-1.x-dev » 8.x-5.x-dev
anpolimus’s picture

Issue tags: +KyivCampCS17
Sarah10’s picture

Thanks!!

Sarah10’s picture

Hi, I have created two plugins and template to implement custom composite element.

  • Then I have created fields, but they are not showing up anywhere.
  • I have enabled the option for "Unlimited values - it still didn't show up.
  • And then I tried unlimited values in the table format. It worked.

What am I doing wrong? Why is it not showing up without table format?

Thanks!

jrockowitz’s picture

StatusFileSize
new97.2 KB

You must configure and check 'Display elements in table columns'.

Sarah10’s picture

Yes, If I check the box "Display elements in table columns" - It is working perfectly fine.

If I do not check the box - Nothing is coming up., It's totally empty like blank screen

Sarah10’s picture

And even if I make it as "Limited" and number of values as "1" - It is showing up a blank screen.

jrockowitz’s picture

@sattapuram You might need to slowly duplicate an existing composite element and figure out what is missing from your custom element.

Sarah10’s picture

I figured it out....It is because of the theme..
Reverting to the core theme solved the issue.

Thanks!!

Sarah10’s picture

@jrockowitz How do I include File upload field in my composite element?

I tried in different ways...the field is showing up but the file is nowhere saved/uploaded.

jrockowitz’s picture

Complex elements (aka entity and file references) are not supported by composites without some hacking.

I am working on #2884624: Allow composite sub elements to be formatted which should help support complex elements with in composites.

gilles.koffmann’s picture

@Sarah10: I have the same problem as you. Field does not show up when it is not in table column.

What do you mean by "Reverting to the core theme solved the issue." ?

I have created my composite element in my own module and I ve basically duplicated the address composite element

I have added in mymodule_theme

function mymodule_theme($existing, $type, $theme, $path) {
  return [
    'webform_composite_participant' => [
      'render element' => 'element',
      'file' => 'includes/mymodule.theme.inc',
    ]
  ];
}

And in the mymodule.theme.inc i have added a preprocess function like this

function template_preprocess_webform_composite_participant(array &$variables) {
  $element = $variables['element'];
  foreach (Element::children($element) as $key) {
    if ($element[$key]['#access']) {
      $variables['content'][$key] = $element[$key];
    }
  }
  $variables['flexbox'] = (isset($element['#flexbox'])) ? $element['#flexbox'] : FALSE;
}

But it does not enter this function.

And finally I have added a template file webform-composite-participant.html.twig in the templates directory of my module

I´m using beta13

n3xo’s picture

How can i create a Custom Composite element? there is docs?

jrockowitz’s picture

There is no documentation. Hopefully someone can contribute some documentation.

lpalgarvio’s picture

Take a look at the example that I've published.
https://github.com/solsoft/Drupal8WebformCompositeExample

I pulled the code from a previous project that I developed and adapted for sharing.
It has not been tested, but should work with no or little changes.

jrockowitz’s picture

@lpalgarvio Your example inspired me to create #2893508: Add webform_example_composite.module. Thanks.

zombree’s picture

StatusFileSize
new69.75 KB

@jrockowitz If I use your webform_example_composite module code, adding the example composite field to a webform and setting that example composite field to have unlimited values, the webform will not submit. I get no error messages, just the form refreshing instead of submitting and loading the confirmation page.

Screen shot of webform field configuration ui, showing the field set to unlimited values.

If I add it as a single value field, it works fine.

Further testing shows the same issue with the Link composite field provided by the Webform module. I have not tested all the composite fields provided, but assume they behave in a similar fashion.

I noticed that in the code for webform_example_composite you specified that Webform composite can not contain multiple value elements (ie checkboxes) or composites (ie webform_address), but it seems that composites themselves cannot be set to accept more than 1 value without breaking the form. Is this supposed to be the case, or is this a bug? I had very much hoped to be able to create a composite that could be set to "Unlimited" values.

I am using Webform 8.x-5.0-beta15, for reference. Thank you for any guidance you can provide.

zombree’s picture

Scrap that. Isn't that the way of it, you test every way to Sunday and then finally post a comment, only to prove yourself wrong a few minutes later.

The lack of form submission was due to something completely unrelated (someone removed all attributes from submit button in input--submit.html.twig), but only happened when I used composite fields with unlimited values. Apologies for false alarm; I'd erase my comment if I could.

kingfisher64’s picture

Would anyone be kind enough to upload a working composite example with basic instructions on how to do this?

I've asked here as it was suggested that documention for custom composite creation should go however this thread appears to the only one where people have had success adopting the example.

Many thanks

zombree’s picture

Hey @kingfisher64, I will post code from my working composite on your question over in this issue: https://www.drupal.org/node/2883734

jrockowitz’s picture

@zombree and @kingfisher64

Actually, the 'here' in https://www.drupal.org/node/2883734 should be linked to https://www.drupal.org/docs/8/modules/webform/webform-cookbook. Fixing it now.

kingfisher64’s picture

@zombree and @jrockowitz - thank you both for your help and advice and kindness.

0sarah0al’s picture

You, guys are awesome!!

Thank you so much..

jrockowitz’s picture

I just committed #2916573: Provide a basic customizable composite element. which makes it easy to create a simple custom composite element via the UI.

There is also the webform_example_composite.module which demonstrate how to create an advanced custom and reusable composite element.

For "composite form" support, I think this feature should be handled in a new ticket.. once someone is ready to build it.

jrockowitz’s picture

jrockowitz’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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