Commit 7a245722 authored by Gorka G.'s avatar Gorka G.
Browse files

Resolve #3240801 "Data loss occurs"

parent 2570142c
Loading
Loading
Loading
Loading
+5 −5
Original line number Diff line number Diff line
@@ -115,7 +115,7 @@ class RatingWidgetBase extends WidgetBase {
    // Check if we are editing the field or is new.
    $is_new = $this->isNewValue($form_state);
    // And get therefore the values depending also if the submit has failed.
    $selected_values = $this->getSelectedOptions($form_state->getUserInput(), $items, $rating_options, $is_new);
    $selected_values = $this->getSelectedOptions($form_state->getUserInput(), $items, $questions, $is_new);
    // And add to the list.
    $element['#values'] = $selected_values;

@@ -211,19 +211,19 @@ class RatingWidgetBase extends WidgetBase {
   *   Array of all the inputs of the user from the form_state.
   * @param \Drupal\Core\Field\FieldItemListInterface $items
   *   The field values.
   * @param array $rating_options
   *   The rating options of the field from the element.
   * @param array $questions
   *   The questions of the field from the element.
   * @param bool $is_new
   *   Flaf to indicate if the field is being edited or not.
   *
   * @return array
   *   Normal array with the inputs of the radios on each row.
   */
  protected function getSelectedOptions(array $user_input, FieldItemListInterface $items, array $rating_options, $is_new) {
  protected function getSelectedOptions(array $user_input, FieldItemListInterface $items, array $questions, $is_new) {
    $values = [];
    // First we check if the field has been submitted and failed.
    // It has the latest value of the field.
    foreach ($rating_options as $key => $option) {
    foreach ($questions as $key => $option) {
      // We just check if the field exists, meaning any radio has been selected.
      if (isset($user_input[$this->fieldName . '-row-' . $key])) {
        // Yes, so we get the value from the input array.