How do you create a link to a modal dialog? The following code does not work:

&lt;div class="topics"&gt;&lt;a class="use-ajax" data-dialog-type="modal" href="/topics-listing"&gt;All Topics&lt;/a&gt;&lt;/div&gt;


Drupal 8.6.5. Bootstrap subtheme. Chrome console log shows no errors. Rather than a modal, the referenced page opens.

So, how do you create a link to a modal dialog in Drupal 8?
This html looks ok, but did you include the core/drupal.dialog.ajax js library that attach event to your link ?

In twig :

{{ attach_library('core/drupal.dialog.ajax') }}


In PHP, it looks like this :

$renderArray = [
            '#title' =&gt; t('All Topics'),
            '#type' =&gt; 'link',
            '#url' =&gt; Url::fromRoute('XXX'),
            '#attributes' =&gt; [
                'class' =&gt; ['use-ajax'],
                'data-dialog-type' =&gt; 'modal',
            ],
            '#attached' =&gt; [
                'library' =&gt; ['core/drupal.dialog.ajax'],
            ],
];