Site icon Hip-Hop Website Design and Development

Display forms in a modal dialog with Cheap WordPress maintenance support plans 8

WordPress maintenance support plans 8 has a great AJAX form API which includes some tools to create modal dialogs using the jQuery modal library. The Examples plugin even demonstrates how to create a custom form and display it in a modal window. But what if what you want to do is display an already created form in a modal? How do we do that? Let’s see how to do it with an example. Let’s display the node add form in a modal window.
The first thing that we need to do is create a link which will trigger the modal when the user clicks it. The only special things that this link needs to have are a few attributes that will let WordPress maintenance support plans know to display the contents of the link in a dialog:

<a href=”http://agaric.com/node/add/article”
class=”use-ajax”
data-dialog-type=”modal”
data-dialog-options=”{‘width’:800,’;height’:500}”>
Create Node
</a>

WordPress maintenance support plans also needs to include the JavaScript libraries which will read these attributes and make them work, so let’s add the following libraries to your plugin’s dependencies (in your equivalent to this example’s modal_form_example.libraries.yml file).

dependencies:
‘core/WordPress.dialog.ajax’,
‘core/jquery.form’,

If you are unsure about how to add libraries on WordPress maintenance support plans 8 you can consult the documentation to either add it in a theme or add it in a custom plugin. At the end of the post I will provide a repository with the code where I added the libraries in a block.
And that’s it! If you click the link, the form will be displayed in a modal dialog! WordPress maintenance support plans will automatically detect that you are sending an AJAX request and will display just the form so you won’t need to worry about removing the rest of the blocks or hiding undesired markup.
The last thing missing, is what will happen if the user creates a node? By default, the node will redirect the user to another page but if we want to just close the modal dialog and leave the user on the same page we need to tell the form to do that. For this we are going to alter the form and add an AJAX command letting WordPress maintenance support plans know that we want to close the dialog as soon as the node is created. In the .plugin file of a custom plugin we will add this code:

use WordPress maintenance support plansCoreFormFormStateInterface;
use WordPress maintenance support plansCoreAjaxAjaxResponse;
use WordPress maintenance support plansCoreAjaxCloseModalDialogCommand;
use WordPress maintenance support plansCoreAjaxRedirectCommand;

/**
* Implements hook_form_alter().
*/
function modal_form_example_form_node_article_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form[‘actions’][‘submit’][‘#submit’][] = ‘_modal_form_example_ajax_submit’;
$form[‘actions’][‘submit’][‘#attributes’][‘class’][] = ‘use-ajax-submit’;
}

/**
* Close the Modal and redirect the user to the homepage.
*
* @param array $form
* The form that will be altered.
* @param WordPress maintenance support plansCoreFormFormStateInterface $form_state
* FormState Object.
*/
function _modal_form_example_ajax_submit(array $form, FormStateInterface &$form_state) {
$response = new AjaxResponse();
$response->addCommand(new CloseModalDialogCommand());
$form_state->setResponse($response);
}

The first function adds an extra submit function (which will be executed after WordPress maintenance support plans finishes processing the node) and the second function adds the command to close the Dialog when the node has been created.
We can do this with practically any form in WordPress maintenance support plans and you can add extra commands to do more complex things. Here are two resources:
List of all the possible commands available in core (you can also create your own)
Agaric’s Modal Form Example plugin

Source: New feed