4

I'm really struggling with this issue. Well I'm having a view, in this view a field configured as personalized text as a link. I want to open a popup form on click on the link. Here is what I did in the method called by the link:

 $response = new AjaxResponse();
  $title = 'Title for the Popup.';
  $form['#attached']['library'][] = 'core/drupal.dialog.ajax';
  $response->setAttachments($form['#attached']);
  $content = '<div class="test-popup-content">' . 'Content in HTML format' . '</div>';
  $options = array(
    'dialogClass' => 'popup-dialog-class',
    'width' => '75%',
  );
  $modal = new OpenModalDialogCommand($title, $content, $options);
  $response->addCommand($modal);
  return $response; 

But It's not working at all! Please help!

1 Answer 1

5

Your code is building an ajax response. This only works in ajax callbacks, nowhere else.

If you want to make a link modal, so that it will open in a popup, you have to set the class use-ajax and the attribute data-dialog-type. Like in this example from ConfigSync.php:

$links['view_diff'] = [
  'title' => $this->t('View differences'),
  'url' => Url::fromRoute($route_name, $route_options),
  'attributes' => [
    'class' => ['use-ajax'],
    'data-dialog-type' => 'modal',
    'data-dialog-options' => json_encode([
      'width' => 700,
    ]),
  ],
];

For modal links to work you need to attach a library, see How to create modal windows (pop-ups)?

4
  • thank you for the answer, but where can I can configure the class and the attribute from a link view?
    – Asy
    Nov 23, 2016 at 9:46
  • The simplest method would be to rewrite the field with the link markup including the class and attribute.
    – 4uk4
    Nov 23, 2016 at 11:05
  • <a class='use-ajax' data-dialog-type='modal' data-accepts="application/vnd.drupal-dialog" data-dialog-options="{"width":500}" href='url> modal </a> but still don't work :(
    – Asy
    Nov 23, 2016 at 22:23
  • 6
    This works: <a class="use-ajax" data-dialog-type="modal" href="http://mysite/node/1">Modal link</a>
    – 4uk4
    Nov 24, 2016 at 7:31

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.