Skip to content
Snippets Groups Projects

Resolve #3512812 "Adding and removing"

1 file
+ 16
5
Compare changes
  • Side-by-side
  • Inline
@@ -88,7 +88,7 @@ class DeriverEditForm extends DeriverFormBase {
// Inline addition of a new deriver.
// Assume getNewDeriverOptions() returns an array of available derivers.
$new_deriver_options = $this->getNewDeriverOptions($form_state);
$new_deriver_options = $this->getNewDeriverOptions($form_state);
$form['derivers_form_wrapper']['new_deriver'] = [
'#type' => 'container',
'#tree' => TRUE, // Preserve nested structure.
@@ -184,10 +184,21 @@ class DeriverEditForm extends DeriverFormBase {
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$test = $form_state->get('attached_derivers');
// Update the entity with the temporary state.
$this->entity->setDerivers($form_state->get('attached_derivers'));
// Save the entity only at this point.
// Retrieve the current temporary state of derivers.
$attached_derivers = $form_state->get('attached_derivers');
// Get the submitted values for each deriver row (which include the updated weight).
$submitted_derivers = $form_state->getValue(['derivers_form_wrapper', 'derivers']);
// Update the temporary state with the new weight values.
foreach ($submitted_derivers as $delta => $values) {
if (isset($attached_derivers[$delta])) {
$attached_derivers[$delta]['weight'] = $values['weight'];
}
}
// Update the entity with the modified derivers and save.
$this->entity->setDerivers($attached_derivers);
$this->entity->save();
$this->messenger()->addStatus($this->t('Deriver %name has been saved.', ['%name' => $this->entity->label()]));
}
Loading