0

It looks like this is a frequent question, but I still can not figure out, why my custom webform element is not showing up. (I read already this post and also the example module)

web/modules/custom/site_registration/src/Plugin/WebformElement/PersonCountElement.php:

<?php

namespace Drupal\site_registration\Plugin\WebformElement;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Annotation\WebformElement;
use Drupal\webform\Plugin\WebformElement\Hidden;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Provides a 'site_person_count' element.
 *
 * @WebformElement(
 *   id = "site_person_count",
 *   label = @Translation("Person Count"),
 *   description = @Translation("Provides a fancy person counter."),
 *   category = @Translation("Custom"),
 * )
 *
 * @see \Drupal\webform_example_element\Element\WebformExampleElement
 * @see \Drupal\webform\Plugin\WebformElementBase
 * @see \Drupal\webform\Plugin\WebformElementInterface
 * @see WebformElement
 */
class PersonCountElement extends Hidden {

  /**
   * {@inheritdoc}
   */
  public function prepare(array &$element, WebformSubmissionInterface $webform_submission = NULL) {
    parent::prepare($element, $webform_submission);

    // Here you can customize the webform element's properties.
    // You can also customize the form/render element's properties via the
    // FormElement.
    //
    // @see \Drupal\webform_example_element\Element\WebformExampleElement::processWebformElementExample
  }

  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    // Here you can define and alter a webform element's properties UI.
    // Form element property visibility and default values are defined via
    // ::defaultProperties.
    //
    // @see \Drupal\webform\Plugin\WebformElementBase::form
    // @see \Drupal\webform\Plugin\WebformElement\TextBase::form
    return $form;
  }


}

web/modules/custom/site_registration/src/Element /PersonCountElement.php:

<?php

namespace Drupal\site_registration\Element;

use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Render\Element\Hidden;

/**
 * Provides a 'site_person_count'.
 *
 * Webform elements are just wrappers around form elements, therefore every
 * webform element must have correspond FormElement.
 *
 * Below is the definition for a custom 'site_person_count' which just
 * renders a simple text field.
 *
 * @FormElement("site_person_count")
 *
 * @see FormElement
 * @see https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21Element%21FormElement.php/class/FormElement
 * @see \Drupal\Core\Render\Element\RenderElement
 * @see https://api.drupal.org/api/drupal/namespace/Drupal%21Core%21Render%21Element
 * @see \Drupal\webform_example_element\Element\WebformExampleElement
 */
class PersonCountElement extends Hidden {

  public function getInfo() {
    $properties = parent::getInfo();

    // $properties['#theme'] = 'input__site_person_count';
    return $properties;
  }

}

Is there anything more to it?

1 Answer 1

0

Ok, this was a really stupid mistake! my src/Element folder had actually a space in the name.

correct: src/Element/PersonCountElement.php
wrong: src/Element /PersonCountElement.php

With the correct folder name, it works like a charm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.