내 사용자 정의 요소가 렌더링되지 않는 이유는 무엇입니까?

Aug 19 2020

라디오 및 확인란 유형의 사용자 지정 Webform 요소를 만들려고했습니다. 이 요소는 관리자 용 Webform UI에서 사용할 수 있습니다. 빌드 섹션에서 내 웹폼에 추가 할 수 있지만 웹폼 프런트 엔드에서 렌더링되지 않습니다. 나는 내가 무엇을 놓치고 있는지 전혀 모른다.

(참고 : 텍스트 필드, 선택 상자, 자동 완성 유형의 다른 사용자 정의 요소를 만들었으며 모두 제대로 작동합니다.)

다음은 내 사용자 지정 src / Element 코드 및 src / Plugin / WebformElement 코드입니다. (이 요소를 프런트 엔드 양식에서 렌더링하려면 다른 파일을 추가 / 수정해야합니까?)

--> src/Element/MyCustomRadio.php

<?php

namespace Drupal\my_custom_element\Element;

use Drupal\Core\Render\Element\Radios;
use Drupal\Core\Render\Element\FormElement;
use Drupal\Core\Form\FormStateInterface;

/**
 * @FormElement("my_custom_element")
 *
 */
class MyCustomRadio extends Radios {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#input' => TRUE,
      '#size' => 60,
      '#process' => [
        [$class, 'processMyCustomRadio'], [$class, 'processAjaxForm'],
      ],
      '#element_validate' => [
        [$class, 'validateMyCustomRadio'], ], '#pre_render' => [ [$class, 'preRenderMyCustomRadio'],
      ],
      '#theme' => 'input__my_custom_element',
      '#theme_wrappers' => ['form_element'],
    ];
  }

  public static function processMyCustomRadio(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add and manipulate your element's properties and callbacks. return $element;
  }

  public static function validateMyCustomRadio(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add custom validation logic. } /** * @param array $element
   * @return array
   */
  public static function preRenderMyCustomRadio(array $element) { $element['#attributes']['type'] = 'checkboxes';
    Element::setAttributes($element, ['id', 'name','value']); static::setAttributes($element, ['form-text', 'my-custom-element']);
    return $element;
  }

}

================================================ ============================================

--> src/Plugin/WebformElement/MyCustomRadio.php
<?php

namespace Drupal\my_custom_element\Plugin\WebformElement;

use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformElement\Radios;
use Drupal\webform\Plugin\WebformElementBase;
use Drupal\webform\WebformSubmissionInterface;

/**
 * Provides a 'my_custom_element' element.
 *
 * @WebformElement(
 *   id = "my_custom_radio_element",
 *   label = @Translation("My Custom Radio"),
 *   description = @Translation("Provides a webform radio element."),
 *   category = @Translation("My Custom elements"),
 * )
 */
class MyCustomRadio extends Radios {

  /**
   * {@inheritdoc}
   */
  protected function defineDefaultProperties() {

    return [
      'multiple' => '',
      'size' => '',
      'minlength' => '',
      'maxlength' => '',
      'placeholder' => '',
    ] + parent::defineDefaultProperties();
  }

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

}

================================================ ============================================

또한 문제를 명확하게 이해할 수 있도록 이미지를 첨부했습니다. 이 모듈은 새로운 drupal 8 인스턴스로 만들어졌습니다. 특정 세부 사항이 필요한 경우 알려주십시오.

관리자 패널에서 : (내 양식에 사용자 지정 라디오를 추가 할 수있는 옵션이 있습니다)

프런트 엔드에서 : (사용자 지정 라디오는 렌더링되지 않고 drupal & contrib 라디오와 확인란 만 렌더링됩니다.)

답변

1 jrockowitz Aug 19 2020 at 08:51

라디오 또는 체크 박스가 작동하려면 전화해야합니다.

\Drupal\Core\Render\Element\Checkboxes::processCheckboxes

또는

\Drupal\Core\Render\Element\Radios::processRadios

예제 코드는 라디오를 확장하지만 확인란을 렌더링하려고합니다. Checkboxes를 확장하고 \ Drupal \ Core \ Render \ Element \ Checkboxes :: processCheckboxes를 포함한 기본 메서드를 점진적으로 재정의하는 것이 좋습니다.

아래는 예입니다

<?php

namespace Drupal\my_custom_element\Element;

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element\Checkboxes;

/**
 * Provides a form element for a set of My custom element.
 *
 * @FormElement("my_custom_element")
 */
class MyCustomElement extends Checkboxes {

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $properties = parent::getInfo(); // My custom properties. $class = get_class($this); $properties['#process'][] = [$class, 'processMyCustomElement']; $properties['#element_validate'] = [[$class, 'validateMyCustomRadio']]; return $properties;
  }

  /**
   * {@inheritdoc}
   */
  public static function processCheckboxes(&$element, FormStateInterface $form_state, &$complete_form) { // You can override and extend this method. $element = parent::processCheckboxes($element, $form_state, $complete_form) return $element;
  }

  public static function processMyCustomRadio(&$element, FormStateInterface $form_state, &$complete_form) { // Here you can add and manipulate your element's properties and callbacks. return $element;
  }

  public static function validateMyCustomRadio(&$element, FormStateInterface $form_state, &$complete_form) {
    // Here you can add custom validation logic.
  }
}