1

Ok so i have a custom entity and want to display a custom delete form. Have created the Route and everything works except for Drupal messenger displaying status message after submission, even after a manual refresh.

The weird thing is, if i add a ksm() output to hook_entity_delete() which is called after the entity is deleted, the status message does display.

I have added a '?destination=' parameter to the end of the link the initiates the popup, and after submit, the redirect is successful.

Here is my class, which i can get to popup in a dialog window, with the form and successful deletion and window close.

Just to clarify, trying to get the status message to display on the form from where the modal popup was initiated from, not to get the message in an additional popup.

Any ideas?

class TrEntryDeleteForm extends ConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $na_entry = NULL) {

    $entry_id = $na_entry->id();

    $form['title'] = [
      '#markup' => t("<p>Title</p>"),
    ];

    $form['description'] = [
      '#markup' => t("<p class='text-center'>This action can't be undone</p>"),
    ];

    $form['entry_id'] = [
      '#type' => 'hidden',
      '#default_value' => $entry_id,
      '#required' => TRUE,
    ];

    $form['actions'] = [
      '#type' => 'actions',
    ];

    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#value' => $this->t('Cancel'),
      '#attributes' => [
        'class' => [
          'use-ajax',
          'cancel',
        ],
      ],
      '#ajax' => [
        'callback' => [$this, 'closeModalForm'],
        'event' => 'click',
      ],
    ];

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this->t('Delete'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    $entry_entity = \Drupal::entityTypeManager()->getStorage('entry')->load($entry_id);
    $entry_entity->delete();

    \Drupal::messenger()->addStatus(
      t('@msg', ['@msg' => 'Entity deleted.'])
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {

  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    // TODO: Implement getCancelUrl() method.
  }

}
3
  • As a workaround, you could try adding the msg inside hook_entity_delete
    – No Sssweat
    Feb 24, 2021 at 6:51
  • Otherwise, try initiating the popup via AJAX submit, just like the first code block of this question
    – No Sssweat
    Feb 24, 2021 at 6:56
  • 1
    The first option only works if i add the devel ksm() output, then the message will display. Will look into the second option. Have also improved the question to make it more obvious what i'm trying to do.
    – Key
    Feb 25, 2021 at 12:27

1 Answer 1

1

After redirection the \Drupal::messenger() may not work. So try with the Drupal OpenModalDialogCommand

use Drupal\Core\Ajax\OpenModalDialogCommand;

/**
* {@inheritdoc}
*/
  public function submitForm(array &$form, FormStateInterface $form_state){
    
    $entry_entity = \Drupal::entityTypeManager()->getStorage('entry')->load($entry_id);
    $entry_entity->delete();

    $response = new AjaxResponse();
    $response->addCommand(new OpenModalDialogCommand("Success", 'Entity deleted.', ['width' => 800]));
    return $response;
  }
1
  • Am i right in assuming that this will display the confirmation message in another popup? I'm trying to get the status message displayed as markup on the form from where the modal was initiated from
    – Key
    Feb 25, 2021 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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