I've tried to implement a custom Webform element, just a simple checkbox. The element is available in the Webform UI, I can add it to my forms, but it never gets rendered. I have no idea what I'm missing. This is my custom plugin code:

<?php

namespace Drupal\mymodule\Plugin\WebformElement;

use Drupal\webform\Plugin\WebformElement\Checkbox;

/**
 * Provides a custom 'checkbox' element.
 *
 * @WebformElement(
 *   id = "my_checkbox",
 *   label = @Translation("Custom checkbox"),
 *   description = @Translation("Provides a custom checkbox form element."),
 *   category = @Translation("Custom elements"),
 * )
 */
class MyCheckbox extends Checkbox {

}

So I'm only extending the existing Webform Plugin 'Checkbox', changing the annotations.

This goes to modules/custom/mymodule/src/Plugin/WebformElement/MyCheckbox.php

But the element isn't rendered in any form I'm adding it to.

I'm using the latest beta version of Webform, beta13. Thanks for any hints!

Regards,
Boris

Comments

drubb created an issue. See original summary.

drubb’s picture

Issue summary: View changes
jrockowitz’s picture

You need to define a corresponding FormElement.

In your example you would need to extend \Drupal\Core\Render\Element\Checkbox to create Drupal\mymodule\Element\MyCheckbox.

@see https://www.slideshare.net/philipnorton42/webform-and-drupal-8
@see https://www.youtube.com/watch?v=xrWEizVqAR4&t=1333s

drubb’s picture

Ah, thanks, I wasn't aware of this. So every Webform element plugin definitly needs a corresponding form element plugin? There's no inheritance in this case?

jrockowitz’s picture

Yes, WebformElement requires a FormElement. Webform elements are wrappers around form elements.

drubb’s picture

Ok, I was mislead some way. I had taken a look at the Webform module itself, and there is a 'Checkbox' class in src/Plugin/WebformElement, but no 'Checkbox' class in src/Element, so I thought the core checkbox element would be used automatically.

Is the connection established by the id in the annotations, so if would use the id 'xyz' and any other module, core or not, would implement a form element 'xyz', it would work?

jrockowitz’s picture

The connection is established by the id which is why all webform elements are prefixed with 'webform_' to prevent any conflicts.

jrockowitz’s picture

Status: Active » Closed (works as designed)