The article discusses the use of AJAX in components, modules and plug-ins Joomla.
Frankly speaking, the native documentation of Joomla is not so hot, and after I was contacted for the last time with a request to add Ajax to the modules and plugins of Joomla 2.5, I decided to write a small topic on how to do it.
First, a little about the structure of the CMS itself: the visual layer consists of a component and a module, where the component is most often the main content of the page, and the modules are small blocks of information, for example, on Habré the blocks on the right (live, Q & A) are the modules, and the main content of the page , for example, the text of a topic with comments or a tape of posts is a component.
In the figure, the red highlights modules, in blue, the componentPlugins are applied to the entire page output. These can be email hardeners, which replace the email address with a picture or javascript, code highlighting, replacing everything in the <code> tag.
If you need to use asynchronous requests in the component (the main content of the site), then the CMS provides such an opportunity. If you have some component, let's say a search by hotels, in which you want to implement fast asynchronous filtering by some parameters, then you can simply add to the address bar “tmpl = component” only the output of the component.
The same page as in the first image, but without modules, only the output of the component .
No need to think that tmpl = component is some kind of magic or hack, for each component or module you can make an unlimited number of templates and apply them depending on any conditions.
So, if we need to add AJAX to the component, we simply call up the component page with a certain parameter and do not return, in our case, the list of hotels by a specific filter, we replace them in the list that the user sees on the screen.
But if we have to add any similar functionality to the module, then we will have to do some feints with our ears. The main difference between a component and a module, the component is an independent unit of content, while a module is only some additional information on the page.
We can get the contents of a component
separately , for example, call a lightbox from one component with the contents of another component, and this will not work with the module, so we will have to contact the file path directly, for example, if we have a small module with a list of hotels, then the URI for AJAX- The call will be approximately /modules/mod_test/ajax.php?action=getHotelList&city=Moscow.
But in order to have access to the Joomla Framework inside this file, we need to include certain files and define some constants:
define('_JEXEC', 1);
_JEXEC is checked in most other CMS files for minimal confirmation that the file is being called inside the framework.
define('JPATH_BASE', dirname(__FILE__) . '/../../..' );
JPATH_BASE is a constant in which the path to the main directory of the site is stored. Since we are not in the root of the site, but in the folder with any plug-in or module, we need to add a “lift” up several levels to the path to the current file.
define('DS', DIRECTORY_SEPARATOR); require_once(JPATH_BASE.DS.'includes'.DS.'defines.php'); require_once(JPATH_BASE.DS.'includes'.DS.'framework.php');
After determining the path we need to connect the main framework files to the CMS file in the same index.php file.
JFactory::getApplication('site')->initialise();
After the application is initialized, we can access the framework API and do what we need, for example, to check if the current user has access to view the list of hotels, get a list from the database and return it in the form of HTML code or JSON.