Octopoos / SEBLOD

Build high-end websites with SEBLOD®, a CCK for Joomla!
https://www.seblod.com
GNU General Public License v2.0
129 stars 54 forks source link

Joomla 4 API not working anymore When SEBLOD 4 is installed #693

Closed olivier-nolbert closed 1 year ago

olivier-nolbert commented 1 year ago

Hi @sebastienheraud ,

API calls on a Joomla 4 website do not work anymore when SEBLOD 4 is installed.

Here what's returned by any calls you make :

Unable to load router: api

After some digging, we found the problem comes from the SEBLOD system plugin which tries to get the current app router but doesn't exist for the 'api' application. Here's what we've implemented to fix it in the 'onAfterInitialise()' method :

Before :

$router =   $app::getRouter();

After :

if ( $app->getName() == 'api' ) {
    return;
}

$router =   $app::getRouter();

With this fix, we're able to make API Calls. But...

When we try to get list of articles, the Joomla API Application triggers content events and so the SEBLOD Content plugin 'onContentPrepare()' method is called. This raises new errors as SEBLOD seems to not know the context and still tries to work with the application Router.

We've implemented the same fix in the 'onContentPrepare()' SEBLOD content plugin method before the core code :

$app    =   JFactory::getApplication();

if ( $app->getName() == 'api' ) {
    return true;
}

With these 2 fixes, we're now able to retrieve content articles with the API. The 'Text' property value of each article in the JSON is of course the SEBLOD tag stuff as no plugin prepare code is executed.

We've tested by adding a custom content plugin which updates the text property of the article when application is API and everything's working fine.

Thx

sebastienheraud commented 1 year ago

Hi @olivier-nolbert,

Thanks. OK included in upcoming rp8 package https://github.com/Octopoos/SEBLOD/commit/9cfcfdda1bdcc809b93c3a657cbb74afb3889842

Saba