0

I have written a Joomla 4 plugin (trigger: onAfterRoute) that changes the layout of the article edit based on the category. This works when creating a new article with a pre-selected category, but upon changing the category in the dropdown of the editor the catid is lost. How can I fetch it in my plugin code ?

    $app = Factory::getApplication();
    $option = $app->input->getCmd('option', '');
    if($option == 'com_content' && $layout == 'edit')
    {
        $contentComponent = Factory::getApplication()->bootComponent('com_content');
        $articlemodel = $contentComponent->getMVCFactory()->createModel('Article', 'Site'); //Get the article model
        if($aid){
            $catid = $articlemodel->getItem($aid)->catid; //Get category of article
        }
        elseif($app->input->getCmd('catid')) {
            $catid = $app->input->getCmd('catid', 0);
        }
        else
        {
            //If category change has occured in the edit form - how to get catid ???
        }
    }

1 Answer 1

0

The plugin code is being executed on the Server side before page is rendered and sent to the browser. When someone changes the category via the drop down of the article that is happening on the client side, in the browser.

In your plugin you are getting the value of category id from the database while processing is happening on the server but the change to the category in the drop down is happening in the browser, thus you have to save category id change and re-process on the server for your plugin to respond to the change or you need to react to the change in the browser using Javascript.

Either way you need some Javascript to run on the client side whenever the category is changed in the browser.

One approach would be a small bit of JS that reacts when the category changes then the normal save/apply processing is triggered and the new category is then saved to the db the form reloads from the server and your existing plugin will be executed and the new layout is how you wish.

Second approach is to have JS save the new category to the article via Ajax, simple Fetch or Joomla's Webservices could do this, and then force a reload of the page to again run your plugins code on the server.

The first approach you risk saving someones changes, other than the category change, when perhaps they didn't want to. Second approach you risk losing any other unsaved changes they might have made before they change the category.

Third approach, and without details of what changes you need to make to the layout it is hard to say if this is practical, but your JS can make the changes to layout based on the category id selected. For example it could make certain fields read only or toggle them as hidden etc.

3
  • But I think that both first and second approach would not work unless you allready have entered a title for the article, and that would often not be the case before changing the category.. Jun 13, 2023 at 8:28
  • That is quite likely so your JS would have to account for that.
    – Irata
    Jun 13, 2023 at 23:38
  • Just also be aware that if the site uses Custom Fields in conjunction with Articles then when an admin is editing an article it will trigger a page reload if the category is changed. The com_fields component sets an onChange trigger on the category field which runs the typeHasChanged function in media/com_fields/js/admin-field-typehaschanged.js. Looking at the approach of com_fields could help you. Jun 17, 2023 at 13:44

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.