1

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?

7
  • 1
    You get the menu id for the currect request. The Ajax uses another request than your normal page load. That is why Joomla is using some default or cached value to determine active menu. You should deliver the current ItemId as param to the ajax request instead if it is important for your modules function. Commented Jan 17, 2020 at 16:56
  • Perfect that works thank you!
    – essbaxter
    Commented Jan 17, 2020 at 18:35
  • @ess Welcome to JSE. Please take the tour. Commented Jan 17, 2020 at 20:35
  • @user please post your resolving advice as an answer, not a comment. When resolutions come in the form of comments, pages end up being "abandoned" then are (needlessly/annoyingly) perpetually recycled by automated scripts in the Stack Exchange system. Please post your advice so that the OP can accept your answer. If you both do these things, you both gain rep points and move toward unlocking more privileges. Commented Jan 17, 2020 at 20:38
  • 1
    @user That happens to all of us. Next time try to craft your comment using speak that explicitly asks for a reply from the OP. Sometimes it is best to finish your kind of comment with a sentence that states you are unclear about the issue and that if your advice is what is desired, then you'll post an answer. Thanks for your contribution. Commented Jan 20, 2020 at 20:25

1 Answer 1

0

You get the menu id for the currect request. The Ajax uses another request than your normal page load. That is why Joomla is using some default or cached value to determine active menu. You should add the current ItemId as parameter to the ajax request itself instead if it is important for your modules function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.