In the submitForm
of a custom form, I have this:
switch ($form_state->getValue('suscribe')) {
case 0:
$iStatus = 1;
break;
case 1:
$iStatus = 3;
$sFileName = 'sites/default/files/_private/bulletins/' . $form_state->getValue('adherent')[1] . ".pdf";
$response = new BinaryFileResponse($sFileName);
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'bulletin.pdf'
);
$form_state->setResponse($response);
break;
}
$storage = \Drupal::entityTypeManager()->getStorage('member');
$id = $form_state->getValue('adherent')[0];
$entity = $storage->load($id);
$entity->status = $iStatus;
$entity->save();
\Drupal::messenger()->addMessage($this->t('Your wish has been recorded.'));
suscribe
is a Yes/No radio button.
$form_state->setResponse($response);
triggers the automatic download of a pdf document.
The message (Your wish...) is correctly displayed in case 0.
In case 1, the message shows only if I refresh the page (or go to another page).
How can I have the message displayed without doing another action after submitting the form in case 1?
By the way, database is correctly updated in both cases.
I'm using Drupal 8.6.10.