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

Drupal 8 Custom form with popup

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

这是我的代码。

        $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;

我应该怎么做才能通过单击 link 打开此表单。

$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,
       ]),
     ],
   );

请查看here了解更多详情。 这里还有一个 link 可能会有所帮助,尽管两者相同但仍在共享。

希望对您有所帮助。