Mengapa elemen khusus saya tidak dirender?

Aug 19 2020

Saya sudah mencoba membuat elemen Webform kustom jenis radio dan kotak centang. Elemen tersedia di UI Formulir Web untuk admin, saya dapat menambahkannya ke formulir web saya dari bagian build tetapi tidak dirender di frontend formulir web. Saya tidak tahu apa yang saya lewatkan.

(Catatan: Saya telah membuat elemen kustom lain dari jenis bidang teks, kotak pilih, pelengkapan otomatis dan semuanya berfungsi dengan baik.)

Di bawah ini adalah kode src / Elemen kustom dan kode src / Plugin / WebformElement saya: (Apakah saya perlu menambahkan / memodifikasi file lain untuk membuat elemen ini dirender pada formulir frontend?)

--> 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;
  }

}

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

Saya juga melampirkan gambar agar bisa membantu diri sendiri untuk memahami masalah dengan jelas. Modul ini dibuat dalam instance drupal 8 baru. Tolong beri tahu saya jika ada detail spesifik yang diperlukan.

Di panel admin: (saya memiliki opsi untuk menambahkan radio khusus di formulir saya)

Di frontend: (Radio Kustom tidak dirender, hanya radio drupal & contrib dan kotak centang yang dirender)

Jawaban

1 jrockowitz Aug 19 2020 at 08:51

Agar radio atau kotak centang berfungsi, Anda perlu menelepon

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

atau

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

Kode contoh Anda, memperluas Radio tetapi mencoba membuat kotak centang. Saya merekomendasikan untuk memperluas Kotak Centang dan secara bertahap mengganti metode default termasuk \ Drupal \ Core \ Render \ Element \ Checkboxes :: processCheckboxes.

Berikut ini contohnya

<?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.
  }
}