I'm trying to use 'use-ajax' class to load a page inside a modal box.

My Page is dynamically created using node id pass on my page controller

My link looks like this

Any idea on how can I achieve my goal?


This is a render array that create a link that open a modal windows.

use Drupal\Core\Url;

public function build() {
$output = array();
$output['overlay_link'] = array(
  '#type' => 'link',
  '#title' => $this->t('Add an activity'),
  '#url' => Url::fromRoute('node.add', ['node_type' => 'event'], ['query' => ['destination' => $destination],
    'absolute' => TRUE,]),
  '#attributes' => [
    'class' => ['use-ajax', 'button', 'secondary'],
    'data-dialog-type' => 'modal',
    'data-dialog-options' => \Drupal\Component\Serialization\Json::encode(['title' => t('Add an activity'), 'width' => 800,]),
 return $output;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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