2

How do I get a confirmation message to show as a modal dialog?

$form['mytable'][$id]['operations']['delete'] = array(
  '#type' => 'submit',
  '#value' => t('Delete'),
  '#name' => 'd' . $entity->ref,
  '#attributes' => array('onclick' => 'if(!confirm("Really Delete?")){return false;}'),
);

I've looked at the Colourbox module and OpenModalDialogCommand but I don't know how to integrate this into #attributes.

1
  • Is this an admin page? Double check that jQuery is included in the page render. If this is an admin page, it may not load by default.
    – BWagner
    Apr 2, 2018 at 14:14

2 Answers 2

1

See https://www.drupal.org/node/2253257, wich would do this by default.

It's pretty easy to do it for your own link, note that the patch there is out of date. Instead of the accepts thing, you simply put data-dialog-type and data-dialog-options on your link attributes. See also https://www.drupal.org/node/2488192 that describes this change and contains lots of examples.

1
  • Thanks @Berdir, I don't really understand, I have changed my code to say: ` '#attributes' => [ 'class' => ['use-ajax'], 'data-dialog-type' => 'modal', 'data-dialog-options' => Json::encode([ 'width' => 700, ]), ],` but now i don't get a message at all. Feb 15, 2016 at 8:54
1

First, add the dependency:

js:
  js/module.js: {}
dependencies:
  - core/jquery
  - core/jquery-ui
  - core/drupal.dialog.ajax

Second, add the use-ajax class to the link:

$form["menu"] = array(
  "#type" => "link",
  "#title" => "Ajouter un menu",
  "#url" => Url::fromRoute("sallamand.footer_menu.add"),
  "#attributes" => [
     "class" => array("use-ajax"),
  ],
);
0

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.