Score:1

Ajax drupal dialog doesn't respect dialog-option

cn flag

I have:

$options['attributes']['class'][] = 'use-ajax';
$options['attributes']['data-dialog-type'] = 'modal';
$options['attributes']['data-dialog-options'] = json_encode([
  'width' => 375,
]);

return [
  '#markup' => Link::createFromRoute(
    t('My link'),
    $route,
    [],
    $options
  )->toString()
];

which generates a link as expected:

<a href="/node/1" class="use-ajax" data-dialog-type="modal" data-dialog-options="{&quot;width&quot;:375}">My link</a>

However when the dialog opens, it always has 800px set as width, how do I get it to open at the width set in the json_encode call?

The opened dialog div looks like this:

<div style="position: fixed; height: auto; width: 800px; top: 310.133px; left: 683px; z-index: 101;" tabindex="-1" role="dialog" class="ui-dialog ui-corner-all ui-widget ui-widget-content ui-front" aria-describedby="drupal-modal" aria-labelledby="ui-id-1">
4uk4 avatar
cn flag
Don't know if this is mandatory, but it might be the dialog options are ignored when you don't define `data-dialog-type`.
cn flag
I've added the rendered markup to the question. The element does specify 800px, so I don't think the theme overrides this.
cn flag
@4k4 I've tried adding a type, and updated the question accordingly. Unfortunately it didn't help.
4uk4 avatar
cn flag
I've tested the code and you need the type, otherwise the modal doesn't open at all. Otherwise the code is OK, can't reproduce the issue.
cn flag
That's really interesting, because the modal opened for me before as well. O_o I'm probably doing something dumb, but I can't figure out what...
No Sssweat avatar
ua flag
Switch to Bartik theme and test there.
cn flag
Bartik is a good idea, unfortunately the menu rendering where I link to the modal from broke there, and I can't be bothered spending time to fix that. I'll probably try to step through the dialog renderer instead and see how it treats the input parameters.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.