Commit fa3e3074 authored by Hillary Lewandowski's avatar Hillary Lewandowski Committed by Nigel Cunningham
Browse files

Issue #3253262 by hilly510: Answers not displaying in formbuilder

parent 02f868aa
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\webform_score\Element;

use Drupal\Core\Render\Element\Checkboxes;

/**
 * Provides a webform element for scored scheckboxes.
 *
 * @FormElement("webform_score_checkboxes")
 */
class CheckboxesQuiz extends Checkboxes {}
+12 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\webform_score\Element;

use Drupal\Core\Render\Element\Radios;

/**
 * Provides a webform element for scored radios.
 *
 * @FormElement("webform_score_radios")
 */
class RadiosQuiz extends Radios {}
+12 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\webform_score\Element;

use Drupal\Core\Render\Element\Textfield;

/**
 * Provides a webform element for scored textfields.
 *
 * @FormElement("webform_score_textfield")
 */
class TextfieldQuiz extends Textfield {}
+0 −7
Original line number Diff line number Diff line
@@ -49,11 +49,4 @@ class CheckboxesQuiz extends Checkboxes implements QuizInterface {
    return 'list';
  }

  /**
   * {@inheritdoc}
   */
  public function getAnswerDataTypeId() {
    return 'list';
  }

}
+46 −10
Original line number Diff line number Diff line
@@ -63,6 +63,15 @@ trait QuizTrait {
   */
  public function form(array $form, FormStateInterface $form_state) {
    $form = parent::form($form, $form_state);
    // Get property values from user input because
    // $form_state->getValue() does not always contain every input's value.
    $user_input = $form_state->getUserInput();
    if (isset($user_input['properties'])) {
      $properties = $user_input['properties'];
    }
    else {
      $properties = $form_state->get('element_properties');
    }

    $form['webform_score'] = [
      '#type' => 'details',
@@ -84,22 +93,49 @@ trait QuizTrait {
      ],
    ];

    $score_plugin = $form_state->getValue('webform_score_plugin');
    $current_score_plugin = $properties['webform_score_plugin'];

    // Fixing issue for scoring methodology form not getting created.
    $score_methodology_option = $score_plugin;
    if (empty($score_plugin) && !empty($current_score_plugin)) {
      $score_methodology_option = $current_score_plugin;
    }

    try {
      $plugin = $this->createWebformScorePlugin($this->configuration, $form_state->getValue('webform_score_plugin'));
      // Get current plugins and its configurations.
      $current_plugin_config = [];
      if ((empty($score_plugin) || ($score_plugin == $current_score_plugin)) &&
        !empty($properties['webform_score_plugin_configuration'])) {

        $current_plugin_config = $properties['webform_score_plugin_configuration'];
      }

      if (!empty($score_methodology_option)) {
        $plugin = $this->createWebformScorePlugin($this->configuration, $score_methodology_option);
        if ($plugin instanceof PluginFormInterface) {
          $form['webform_score']['webform_score_plugin_configuration'] = [
            '#tree' => TRUE,
            '#parents' => ['properties', 'webform_score_plugin_configuration'],
          ];

        $sub_form_state = SubformState::createForSubform($form['webform_score']['webform_score_plugin_configuration'], $form, $form_state);
          // Set current plugins and its configurations in form_state to capture it later.
          $form_state->set('current_plugin_config', $current_plugin_config);

          $sub_form_state = SubformState::createForSubform($form['webform_score']['webform_score_plugin_configuration'], $form, $form_state);
          $form['webform_score']['webform_score_plugin_configuration'] += $plugin->buildConfigurationForm([], $sub_form_state);

          // Set the values based on previous config.
          foreach ($current_plugin_config as $index => $config) {
            if (empty($form['webform_score']['webform_score_plugin_configuration'][$index]['#value'])) {
              $form['webform_score']['webform_score_plugin_configuration'][$index]['#value'] = $config;
            }
          }

          WebformElementHelper::setPropertyRecursive($form['webform_score']['webform_score_plugin_configuration'], '#access', TRUE);
        }
      }
    }
    catch (PluginException $e) {
      $this->logException($e);
    }
Loading