2017-05-17 66 views
1

我想在弹出窗口中显示我的自定义窗体,当用户点击前端链接时,我尝试了很多在网络中显示但不适用于我的解决方案。Drupal 8自定义窗体弹出

这是我的代码。

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

我该怎么做,只需点击一个链接即可打开此表单。

回答

3
$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这可能会有所帮助,虽然两者是相同的但仍然共享。

希望这会有所帮助。