Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #2
drubbComment #3
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedYou 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
Comment #4
drubbAh, 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?
Comment #5
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedYes, WebformElement requires a FormElement. Webform elements are wrappers around form elements.
Comment #6
drubbOk, 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?
Comment #7
jrockowitz CreditAttribution: jrockowitz as a volunteer commentedThe connection is established by the id which is why all webform elements are prefixed with 'webform_' to prevent any conflicts.
Comment #8
jrockowitz CreditAttribution: jrockowitz as a volunteer commented