0

When calling an AJAX Method in a Module Helper file the method has to be static. I have no app context there, when doing the following:

public static function getDataAjax()
    {
        $data   = ExampleHelper::getJArticles();
        return $data;
    }

public static function getJArticles(){
        $app = Joomla\CMS\Factory::getApplication();
        $model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);
        $articles = $model->getItems();
        return $articles;
}

An error 500 got thrown: Call to a member function get() on null

#0 /Users/MyUser/Sites/joomla5.site/libraries/src/MVC/Model/ListModel.php(222): Joomla\Component\Content\Site\Model\ArticlesModel->getListQuery()

#1/Users/MyUser/Sites/joomla5.site/libraries/src/MVC/Model/ListModel.php(271): Joomla\CMS\MVC\Model\ListModel->_getListQuery()

#2 /Users/MyUser/Sites/joomla5.site/components/com_content/src/Model/ArticlesModel.php(651)...

There is a similar question here (about getting articles in the backend) (Link) but as far as i can see the solution there does not help me.

I would like to use the model rather then doing my own database query here...

Edit: The AJAX call is done via com_ajax:

...
async loadData() {
        let requestString = this.prepareRequest();
        const res = await fetch(requestString);
        if (res.ok) {
            let parsed = await res.json();
            if (parsed.data && parsed.data.length > 0) {
                this.data = parsed.data;
            }
        }
        return this.data;
    }

    prepareRequest() {
        if (!this.checkConfiguration()) return;
        let requestString = 'index.php?option=com_ajax&module=' + this.moduleName + '&method=' + this.method + '&format=json';
        if (this.backendModule) {
            requestString = 'administrator/' + requestString;
        }
        requestString += '&limit=' + this.limit + '&page=' + this.page;
        return requestString;
    }

    checkConfiguration() {
        return !!(this.moduleName && this.method);
    }
...
3
  • 1
    The com_ajax component will not pass any context. You need boot the component inside your ajax method of your helper class. By the way, the method not necessary need to be static. Commented May 8 at 21:24
  • 1
    Also, I saw you pass some data in your fetch. If you need the request data you must go through the Joomla application's input object, inside your ajax method Commented May 8 at 21:27
  • 1
    You need to set params state on the model. You can see the code in the linked question. I think my answer applies here too.
    – Sharky
    Commented May 9 at 6:24

1 Answer 1

1

As mentioned by Sharky i had to set the params state in de model. The feedback provided by Carlitorweb about the data that got send to the Ajax Method is also true (was not yet implemented). Working (full) example below:

public function getDataAjax()
    {
        $app = Factory::getApplication();
        $input       = $app->input;
        $preferences = array();
        $preferences['limit']        = $input->get('limit', 20, 'integer');
        $preferences['page']        = $input->get('page', 1, 'integer');

        $data   = ExampleHelper::getJArticles($app, $preferences);

        return $data;
    }

    private function getJArticles(SiteApplication $app, array $preferences = [])
    {
        $model = $app->bootComponent('com_content')->getMVCFactory()->createModel('Articles', 'Site', ['ignore_request' => true]);
        // Set the params state in the model
        $model->setState('params', clone ComponentHelper::getParams('com_content'));
        $model->setState('list.limit', $preferences['limit']);
        $model->setState('list.start', ($preferences['page'] - 1) * $preferences['limit']);
        $articles = $model->getItems();
        [...]
        return $articles;
}

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.