0

I have created a custom webform element for radios, however I'm not clear how to set the number of options to minimum of 4 and maximum of 4, so always 4 options. I also want to set the value of each option to increment starting at 1 and for this field to be read-only, so only the title and description for each radio is editable.

I need this because I want to prevent the content editors from messing this up, this is for a questionaire and each section (options element) must have 4 answers (radios).

Maybe I would be better making a composite field with 4 single radio fields?

Anyway, In my file 'my_custom_module/src/Plugin/WebformElement/my_custom_field.php', I tried the following in my form method, but this doesn't work. Also whenever I try to give a default value to options the UI changes to yaml.

<?php

namespace Drupal\my_custom_module\Plugin\WebformElement;

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

/**
 *
 * @WebformElement(
 *   id = "my_custom_field",
 *   api = "https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Render!Element!Radios.php/class/Radios",
 *   label = @Translation("Custom Radio Options"),
 *   description = @Translation("Provides a form element for a set of radio buttons."),
 *   category = @Translation("Options elements"),
 * )
 */
class my_custom_field extends Radios {

  /**
   * {@inheritdoc}
   */
  public function getDefaultProperties() {
    $properties = [
        'required' => true,
      ] + parent::getDefaultProperties();
    unset(
      $properties['options_display'],
      $properties['options_randomize']
    );
    return $properties;
  }


  /**
   * {@inheritdoc}
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);

    $form['element']['multiple']['#min'] = 4;
    $form['element']['multiple']['#min'] = 4;

    return $form;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.