Display forms in a modal dialog with Drupal maintenance support plans 8

Drupal 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 module 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 Drupal 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>

Drupal 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 module’s dependencies (in your equivalent to this example’s modal_form_example.libraries.yml file).

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

If you are unsure about how to add libraries on Drupal maintenance support plans 8 you can consult the documentation to either add it in a theme or add it in a custom module. 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! Drupal 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 Drupal maintenance support plans know that we want to close the dialog as soon as the node is created. In the .module file of a custom module we will add this code:

use Drupal maintenance support plansCoreFormFormStateInterface;
use Drupal maintenance support plansCoreAjaxAjaxResponse;
use Drupal maintenance support plansCoreAjaxCloseModalDialogCommand;
use Drupal 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 Drupal 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 Drupal 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 Drupal 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 module

Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Display forms in a modal dialog with Drupal maintenance support plans 8

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.