Hi there,

To start, i'm fairly new to D8 and have basic OO knowledge. I'm not feeling very confident with building custom code for D8 (yet) but i'm eager to learn.

Now to my question. I want to build a custom FormElement to use within a new form created with YAML form. Thus far i have tried creating my own FormElement Class in src/Element namespace while using YamlFormOptions as an example. Somehow i am unable to make this new element appear in the [Add element] popup.

I also found http://bit.ly/2e9dZ1L but that doesn't seem to work either.

Some pointers would be very much appreciated. Perhaps this support request could lead into some general documentation.

Comments

ericmulder1980 created an issue. See original summary.

ericmulder1980’s picture

Issue summary: View changes
jrockowitz’s picture

Adding an element to the YAML Form module requires two plugins...

First, you need to create a render (form) element. Here are most of the elements that ship with core. https://api.drupal.org/api/drupal/namespace/Drupal%21Core%21Render%21Ele...

Second, you need to define a YamlFormElement plugin that integrates the render element with the YAML Form module.

Below is the Toggle (toggle) element's render element and plugin

A few concepts and tips...

  • Almost all custom render elements should extend an existing html element. For example an HTML editor element would extend a Textarea element.
  • Elements within the YAML Form module are prefixed with YamlForm* and yamlform_* to prevent namespace conflicts and there is some magic that converts '#type': toggle to '#type': yamlform_toggle. You may or may not want to use the YamlForm module's prefixing.
  • There is no documentation about creating these basic form elements. It is worth examining simple HTML elements like the 'textfield' and 'select'.
  • The YamlFormOptions element is an internal element, very complex, and I don't recommend using it as a starting point

BTW, what type of element are you looking to create?

ericmulder1980’s picture

Hi Jacob,

Thank you so much for taking the time to answer my question. This really helps me.

The project i am working on is best explained by comparing it to relationship tests you can find in magazines. You receive a set of questions (theses), one per page, where you need to pick one answer that best suits your situation. Every answer receives a fixed score so at the end of the survey you will have a high or low total score. Additional the questions need to be tagged with a group which makes it possible to get a total score for only a subset of questions. Based on this score i will need to implement some Rules actions to redirect a user to a specific node. I want to do this in Rules because the business logic can change over time.

It has some overlap with the Likert form element. The difference here is that i don't want people to choose a rating per question but i want to add a rating to the answer.

I will keep this issue updated with my progress for others to benefit from.

jrockowitz’s picture

The YAML Form module does not have full Rules integration, especially for submitted data but you obviously know how to code so you should look into the YamlFormHandler plugin, which be used to manage all your custom business logic.

fenstrat’s picture

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

Status: Active » Closed (works as designed)

Closing this ticket. It is also worth noting you can also add custom properties to an element or alter an existing element.

@see #2830042: Custom Element Properties

AOUSDM’s picture

Did someone try this on Drupal 8? Is there any other operations needed?
Just adding the two snippets crashes the site. What am I missing?

unsettlingtrend’s picture

Is #3 still considered to be the proper way to go about creating custom form elements? I just want to make sure it hasn't changed since the YAMLForm/Webform merger; I'm trying to follow your example but still can't get my element to show up.

I'm trying to make a new composite element, and I was looking at the WebformAdress classes for guidance; does it make sense to extend the WebformCompositBase classes to do this?

unsettlingtrend’s picture

I found your code. :-) http://cgit.drupalcode.org/webform/tree/modules/webform_example_composite

I'm expecting this will help; going to leave this comment though for anyone else.