{"id":56311,"date":"2023-01-03T16:46:06","date_gmt":"2023-01-03T11:16:06","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=56311"},"modified":"2023-01-11T17:03:36","modified_gmt":"2023-01-11T11:33:36","slug":"loosely-coupled-open-drupal-ajax-dialog-boxes-from-react-via-drupal-behaviour","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/loosely-coupled-open-drupal-ajax-dialog-boxes-from-react-via-drupal-behaviour\/","title":{"rendered":"Loosely Coupled: Open Drupal Ajax dialog boxes from React via Drupal Behaviour"},"content":{"rendered":"<p>Dialogs are a quick and easy way to display additional information without reloading the entire page. Dialog boxes can display just some static text, any node or form of your page, a views page, or any custom markup you&#8217;ll feed them.<\/p>\n<p>In order to open some link\/node\/page in a modal window, you can add the following structure to the link within React:<\/p>\n<pre>class=\"use-ajax\" data-dialog-type=\"modal\"<\/pre>\n<p>Optionally you can also add <strong>data-dialog-options='{&#8220;width&#8221;:810, &#8220;dialogClass&#8221;: &#8220;modal-react&#8221;}&#8217;<\/strong> to style the modal.<\/p>\n<p>And ultimately, the link will look like this:<\/p>\n<pre>&lt;a\r\n  href=\"\/admin\/content\"\r\n  className=\"use-ajax\"\r\n  data-dialog-type=\"modal\"\r\n  data-dialog-options='{\"width\":810,\"dialogClass\": \"modal-react\"}'\r\n&gt;Dialog link&lt;\/a&gt;<\/pre>\n<h2><strong>Click on this link, and in most cases it will open the dialog.<\/strong><\/h2>\n<p>But if in case the &lt;a&gt; tag is rendered after a few seconds of page load, within a popup, or after some AJAX call, clicking on the link will do nothing. To overcome this, we will use drupal behaviors to Ajaxify the links.<\/p>\n<p>Drupal has a \u201cbehaviors\u201d system to provide a modular and better way for attaching JavaScript functionality to place elements on a page. This Drupal Behavior is the object in JavaScript that Drupal initializes after the page loads and after every AJAX call in DOM.<br \/>\nThis drupal behavior can be called from React, let&#8217;s see how:<\/p>\n<h3>Step 1: Add Drupal Behaviour<\/h3>\n<p>First, we will create drupal behavior and write the code to Ajaxify the link within this. For this, create a file <strong>js\/custom.js<\/strong> and add the following code:<\/p>\n<pre>(function ($, Drupal, drupalSettings) {\r\n  Drupal.behaviors.custom_js = {\r\n    ajaxifyLinks: function () {\r\n      jQuery('.use-ajax:not(.ajax-processed)').addClass('ajax-processed').each(function () {\r\n        var ajaxSettings = {\r\n          url: jQuery(this).attr('href'),\r\n          element: this\r\n        };\r\n        Drupal.attachBehaviors(this, ajaxSettings);\r\n      });\r\n    },\r\n    attach: function (context, settings) {\r\n      \/\/Some JS Code\r\n    }\r\n  }\r\n})(jQuery, Drupal, drupalSettings);<\/pre>\n<p>Since we are getting new content after the page is loaded, once the new content is loaded, we call <strong>Drupal.attachBehaviors()<\/strong> to re-attach any javascript effects to this newly loaded content.<\/p>\n<h3>Step 2: Update our libraries<\/h3>\n<p>Add <strong>custom.js before<\/strong> attaching the <strong>react bundle file<\/strong>. This is to ensure our drupal behavior function <strong>ajaxifyLinks<\/strong> is available when react is loaded.<\/p>\n<pre>react-lib:\r\n  version: 1.x\r\n  js:\r\n    js\/custom.js: {}\r\n    js\/react-app\/dist\/app.bundle.js: { minified: true }<\/pre>\n<h3>Step 3: Call drupal behavior from react<\/h3>\n<p><strong>Drupal.behaviors<\/strong> is a global variable that can be used directly within the React app. custom_js is the behavior name we created in the <strong>custom.js<\/strong> file, and <strong>ajaxifyLinks<\/strong> is a function within that file.<\/p>\n<pre>Drupal.behaviors.custom_js.ajaxifyLinks();<\/pre>\n<p>Call this after new content is loaded like in the below example link will be rendered after 2s, and drupal behavior will be called after that.<\/p>\n<pre>import React, { useState, useEffect } from \"react\";\r\n\r\nconst App = () =&gt; {\r\n  const [timer, setTimer] = useState(false);\r\n\r\n  useEffect(() =&gt; {\r\n    setTimeout(() =&gt; {\r\n      setTimer(true);\r\n    }, 2000);\r\n  }, [])\r\n\r\n  useEffect(() =&gt; {\r\n    Drupal.behaviors.custom_js.ajaxifyLinks();\r\n  }, [timer]);\r\n\r\n  return (\r\n    &lt;&gt;\r\n      {timer &amp;&amp; &lt;a\r\n        href=\"\/admin\/content\"\r\n        className=\"use-ajax\"\r\n        data-dialog-type=\"modal\"\r\n        data-dialog-options='{\"width\":810,\"dialogClass\": \"modal-react\"}'\r\n      &gt;Dialog link&lt;\/a&gt;}\r\n    &lt;\/&gt;\r\n  )\r\n}\r\nexport default App;<\/pre>\n<p>Read More: <a href=\"https:\/\/www.tothenew.com\/blog\/loosely-coupled-pass-data-to-react-app-via-drupalsettings\/\">Loosely Coupled: Pass data to React app via drupalSettings<\/a><\/p>\n<p><strong>Click on the link now, and it will open the dialog.<\/strong><\/p>\n<h3>Optional: Passing parameters<\/h3>\n<p>Parameters can also be passed <strong>from react to drupal behaviors<\/strong>, like for example if we need to pass parent class from react, we can do so:<\/p>\n<pre>Drupal.behaviors.custom_js.ajaxifyLinks('.parent-class');<\/pre>\n<p>And can access it within drupal behaviors like so:<\/p>\n<pre>(function ($, Drupal, drupalSettings) {\r\n  Drupal.behaviors.custom_js = {\r\n    ajaxifyLinks: function (parentClass) {\r\n      console.log(parentClass);\r\n    },\r\n    attach: function (context, settings) {\r\n      \/\/Some JS Code\r\n    }\r\n  }\r\n})(jQuery, Drupal, drupalSettings);<\/pre>\n<p><strong>Note:<\/strong> If any changes are not reflected on page refresh, try clearing the browser or Drupal cache or <strong>unchecking \u201cAggregate JavaScript files\u201d<\/strong> from <strong>\/admin\/config\/development\/performance<\/strong>.<\/p>\n<p>More on <a href=\"https:\/\/www.drupal.org\/docs\/drupal-apis\/ajax-api\/ajax-dialog-boxes\" target=\"_blank\" rel=\"noopener\">Ajax Dialog boxes<\/a>.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>Dialogs are a quick and easy way to display additional information without reloading the entire page. Dialog boxes can display just some static text, any node or form of your page, a views page, or any custom markup you&#8217;ll feed them. In order to open some link\/node\/page in a modal window, you can add the [&hellip;]<\/p>\n","protected":false},"author":1124,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":26},"categories":[3602,3038],"tags":[5071,4862,5072,5067,4064,5073],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56311"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1124"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=56311"}],"version-history":[{"count":2,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56311\/revisions"}],"predecessor-version":[{"id":56391,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/56311\/revisions\/56391"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=56311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=56311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=56311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}