I've got a custom module which makes use of com_ajax to return values without refreshing the page.
In my helper file I'm struggling to get the module parameters within the AJAX request and it looks like it's because the menu item that the AJAX is getting is one level higher up than the menu item that the module is attached to.
To test this, I've stripped everything else out and left it like so:
mod_mymodule/mod_mymodule.php
$data = ModMyModuleHelper::getData();
mod_mymodule/helper.php
getData() {
$active_item = JFactory::getApplication()->getMenu()->getActive()->id;
var_dump($active_item); // this returns the correct menu item ID
}
getAjax() {
$active_item = JFactory::getApplication()->getMenu()->getActive()->id;
var_dump($active_item); // this returns the menu item ID of the parent item
}
mod_mymodule/tmpl/default.php
# Print out the response from the getData() method in the helper.php
<?php echo $data; ?>
# Triggers a call to the ajax request, output is returned to the console
<button onclick="getDynamicData()">Click To Run Ajax</button>
mod_mymodule/module.js
function getDynamicData() {
jQuery.ajax({
url: "index.php?option=com_ajax&module=mymodule&format=json",
type: "POST",
data: "testing",
success: function(data) {
console.log(data);
}
});
}
What I'm struggling with is why when the helper.php is called from the module template it returns the correct menu item, but when it's called via com_ajax it returns the parent item id.
Output to the page on page load: ID 1352 (current item ID) Output in console after clicking button: ID 331 (parent item ID)
This prevents access to the module params as the com_ajax call doesn't see the module assigned to the page as it's looking at a level up.
Any advice on why the menu items are different?