Drupal 8自定义表单,带弹出窗口

Udi*_*wat 2 drupal drupal-8 drupal-modules

当用户点击前端的链接时,我想在弹出窗口中显示我的自定义表单,我尝试了很多在网络上显示的解决方案,但对我不起作用.

这是我的代码.

        $response = new AjaxResponse();
        // Get the modal form using the form builder.
        $modal_form = $this->formBuilder->getForm('Drupal\fwsactions\Forms\FwsActionsForm');
        $modal_form['#attached']['library'][] = 'core/drupal.dialog.ajax';
        // Add an AJAX command to open a modal dialog with the form as the content.
        $modal_form = render($modal_form);
        $response->addCommand(new OpenModalDialogCommand('My Modal Form', $modal_form, ['width' => '800']));

        return $response;
Run Code Online (Sandbox Code Playgroud)

如何通过单击链接打开此表单应该怎么做.

小智 5

$content['overlay_link'] = array(
     '#type' => 'link',
     '#title' => $this->t('Add movie'),
     '#url' => Url::fromRoute('node.add', ['node_type' => 'content_type_movie']),
     '#attributes' => [
       'class' => ['use-ajax'],
       'data-dialog-type' => 'modal',
       'data-dialog-options' => Json::encode([
         'width' => 700,
       ]),
     ],
   );
Run Code Online (Sandbox Code Playgroud)

请查看此处了解更多详情.这是另外一个可能有用的链接,尽管两者相同但仍然共享.

希望这可以帮助.