Skip to content

Commit

Permalink
Fix statistical design issue when uploading files to the design entity
Browse files Browse the repository at this point in the history
  • Loading branch information
paul121 committed Feb 20, 2024
1 parent 79b5a31 commit c8c6673
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed

- Fix managed permissions to edit taxonomy terms. [#622](https://github.com/Rothamsted-Ecoinformatics/farm_rothamsted/issues/622)
- Fix statistical design issue when uploading files to design entity. [#616](https://github.com/Rothamsted-Ecoinformatics/farm_rothamsted/issues/616)

## [2.18.0](https://github.com/Rothamsted-Ecoinformatics/farm_rothamsted/milestone/39) 2023-12-11

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\farm_rothamsted_experiment_research\Form;

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Form\FormStateInterface;

/**
Expand Down Expand Up @@ -283,17 +284,29 @@ public function form(array $form, FormStateInterface $form_state) {
];
}

// Add ajax.
// Add ajax to update the statistical design field after changing the
// selected blocking structure.
$form['statistical_design']['#attributes']['id'] = 'statistical-design-wrapper';
$form['blocking_structure']['widget']['#ajax'] = [
'callback' => [$this, 'blockingStructureCallback'],
'wrapper' => 'statistical-design-wrapper',
'event' => 'change',
];

// Build options.
// Build options from the selected blocking structure.
// Check if a value has been set to the form state storage (not form state
// values) via AJAX before using entity's current field value.
// This prevents errors from happening when other elements reset the form
// state, like file uploads. See issue #616.
$blocking_structure = $form_state->get('blocking_structure') ?? $this->entity->get('blocking_structure')->value;
if (($trigger = $form_state->getTriggeringElement()) && NestedArray::getValue($trigger['#array_parents'], [0]) == 'blocking_structure') {
$blocking_structure = $trigger['#value'];
}
// Save the blocking structure to form state storage.
$form_state->set('blocking_structure', $blocking_structure);

// Load the statistical design options.
$options = [];
$blocking_structure = $form_state->getValue(['blocking_structure', 0, 'value']) ?? $this->entity->get('blocking_structure')->value;
if ($blocking_structure) {
$options = farm_rothamsted_experiment_research_statistical_design_options($blocking_structure);
}
Expand Down

0 comments on commit c8c6673

Please sign in to comment.