esod
esod

Reputation: 345

How to open a modal in Drupal 8 without using a link?

The modal isn't triggered by a link on the page which the user has clicked. The modal is triggered when the user arrives on the url.

Think of something like a disclaimer that pops up as soon as the user arrives on the url.

Upvotes: 8

Views: 6715

Answers (1)

jhedstrom
jhedstrom

Reputation: 3388

You can use the Drupal.dialog function for this.

For example:

var $myDialog = $('<div>My dialog text</div>').appendTo('body');
Drupal.dialog($myDialog, {
  title: 'A title',
  buttons: [{
    text: 'Close',
    click: function() {
      $(this).dialog('close');
    }
  }]
}).showModal();

See node.preview.js for another example.

Update: To use this with an AJAX request/response:

Drupal.ajax({
  url: 'some/path',
  success: function(response) {
    var $myDialog = $('<div>' + response.data + '</div>').appendTo('body');
    Drupal.dialog($myDialog, {title: 'Some title'}).showModal();
  }
}).execute();

Upvotes: 10

Related Questions