0

I have added a button to the node add form which allows the user to import data from the last node created into this one. Within hook_form_alter, I have:

// Import previous month's data if requested
$trigger = $form_state->getTriggeringElement(); //Check to see if button pressed

if ($trigger['#name'] == 'import_previous') {
  $id = \Drupal::request()->query->get('field_patient'); //grab node id from url
  $previous = st_access_patient_get_previous($id); //grab actual node
  $previous_array = $previous->toArray(); //cast node to array

  // Match up data from previous node to fields in this form, set default value
  foreach ($previous_array as $label => $field) {
    if (substr($label, 0, 5) == 'field' && $form[$label]['#access'] == TRUE) {

      if (array_key_exists('#default_value', $form[$label]['widget'])) {
        if (array_key_exists('target_id', $field[0])) {
          $form[$label]['widget']['#default_value'] =  $field[0]['target_id'];
        } else {
          $form[$label]['widget']['#default_value'] =  $field[0]['value'];
        }
      } else {
        if (array_key_exists('target_id', $field[0])) {
          $form[$label]['widget'][0]['value']['#default_value'] = $field[0]['target_id'];
        } else {
          $form[$label]['widget'][0]['value']['#default_value'] = $field[0]['value'];
        }
      }

    }
  }

}

$form['previous_import'] = [
  '#type' => 'button',
  '#name' => 'import_previous',
  '#value' => t("Import the previous month's values"),
  '#weight' => -9999,
  '#submit' => ['import_previous_submit'],
];

My understanding is that the button will just submit the form, I don't actually need

'#submit' => ['import_previous_submit']

When pressing 'import_previous' button the field values are not updated. If I remove the conditional (which just runs the replacement logic on every form load) then everything works fine. So, I know my logic for the replacement itself is correct. But once I wrap my replacement logic in:

if ($trigger['#name'] == 'import_previous') {}

Then the fields no longer update when the button is pressed. The conditional passes, so I know that is correct. Something else is keeping the form default values from being set. I have tried to add:

  $form_state->setRebuild(TRUE);
  $form_state->setCached(FALSE);

after:

$trigger = $form_state->getTriggeringElement();

and directly before the final closing tag of the conditional. I have also tried to add it to a submit function 'import_previous_submit'. No love with any of that. Any help is greatly appreciated.

2

You don't need to check the triggering element if you use it's own submit handler because the submit handler is only triggered by this element.

function your_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  $form['previous_import'] = [
    '#type' => 'submit',  // changed to submit
    '#name' => 'import_previous',
    '#value' => t("Import the previous month's values"),
    '#weight' => -9999,
    '#submit' => ['import_previous_submit'],
  ];
}

function import_previous_submit(&$form, FormStateInterface $form_state) {
  // Example of altering / setting the field values. You have to adapt your foreach stuff.
  $user_input = $form_state->getUserInput();
  $user_input['title'] = [['value' => 'test']];
  $form_state->setUserInput($user_input);

  $form_state->setRebuild();
}
0

Sorry, I forgot that when submitting a form that 'default_value' is no longer in play, I need to be using 'value'. Have been looking at this for too long.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.