2

I have a module that creates thumbnails from the main image and over time the hosting fills with images, for that reason I want to add a maintenance button that deletes the previous images. It is from the administrator, not from the front-end. I have implemented ajax, but for some reason it doesn't work for me and it sends me the following message in broswer console:

Object { success: false, message: "The file at mod_my_module/helper.php does not exist.", messages: null, data: null }

I need your help, I leave the code used in module.

helper.php

use Joomla\String\StringHelper;

$com_path = JPATH_SITE . '/components/com_content/';

JLoader::register('ContentHelperRoute', $com_path . 'helpers/route.php');
JModelLegacy::addIncludePath($com_path . 'models', 'ContentModel');

abstract class ModMyModuleHelper
{
    public  function deleteImagesAjax() {
        $minified_path = JPATH_ROOT . '/cache/mod_my_module_images';
        if(JFolder::exists($minified_path)) {
            JFolder::delete($minified_path);
        }
        die();
    }
}

cleanimages.php This file is a custom field type for create button to delete images from module settings

jimport('joomla.form.formfield');

class JFormFieldCleanimages extends JFormField {

    protected $type = 'Cleanimages';

    protected function getInput() {

        Jhtml::_('jquery.framework');
        $doc = JFactory::getDocument();
        $doc->addScriptDeclaration('jQuery(function($) {
            $("#btn-reset-css").on("click", function(event) {
                event.preventDefault();
                var $this = $(this);
                $this.text($this.data("loading"));
                $.ajax({
                    url: "index.php?option=com_ajax&module=my_module&method=deleteImages&format=json",
                    type   : "POST",
                    success: function (data) {
                        $this.text($this.data("text"));
                        console.log(data);
                    }
                });
                
            });
        });');

        return '<button id="btn-reset-css" class="btn btn-info" data-text="'. JText::_('Cleaned Images') .'" data-loading="'. JText::_('Cleaning Images') .'">'. JText::_('Clean Images') .'</button>';
    }
}

UPDATE Doing my own research and reviewing the component I have discovered that com_ajax looks for the helper.php file using JPATH_BASE so when executing the button in the administrator it would be looking for helper.php inside the /administrator/modules/mod_my_module/helper.php folder , If JPATH_BASE were changed to JPATH_ROOT, this would work in both admin and front-end but it is core and we cannot change it. Given this, is there any way to do it without having to create a module for the administrator?

UPDATE 2 I have solved the problem by calling the full url from the front-end, the code below in the answers.

2

Your AJAX URL is relative and ends up pointing to administrator/index.php. You can change that by prepending root path. First, load the required dependencies:

JHtml::_('behavior.core');

Then change the URL:

url: Joomla.getOptions("system.paths").root + "/index.php?option=com_ajax&module=my_module&method=deleteImages&format=json",
1
  • Thank you, I will mark your answer as the correct one, but I will also leave the solution that I found to this problem. – Leoalv Mar 28 at 19:58
1

Thanks to those who cooperated on this issue.

I had solved this by calling the public url (full url) of the module from the administrator, without letting JPATH_BASE fill in the location. For which modify the url to call by:

url: "'.Uri::root().'index.php?option=com_ajax&module=my_module&method=deleteImages&format=json",

You must add this above use Joomla\CMS\Uri\Uri;

Maybe it will be helpful to someone else

0

Am I understanding correctly that your module is on the front end, but you want to call a helper function from the back end?

If yes, I wonder if JPATH_ADMINISTRATOR might do the trick for you?

https://docs.joomla.org/Constants

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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