1

Could someone kindly help please?

I'm creating a plugin and part of its job is to automatically add a 'Menu Class Suffix' to a chosen menu. I know it can be done manually from the advanced tab of the menu module but I need the plugin to do it if possible.

How can I add a value to 'class_sfx' in 'params' of the menu module in the database table __modules please?

{"menutype":"mainmenu","base":"","startLevel":1,"endLevel":0,"showAllChildren":1,"tag_id":"","class_sfx":"","window_open":"","layout":"_:default","moduleclass_sfx":"","cache":1,"cache_time":900,"cachemode":"itemid","module_tag":"div","bootstrap_size":"0","header_tag":"h3","header_class":"","style":"0"}

Many thanks!

2

Although you could do this by direct database operations, when performing CRUD operations on records of Joomla core components it's best to use the Joomla Table class functionality, as this enables any associated data (eg data which is managed by plugins which register to receive notifications of record updates) to be updated. You can read about Table functionality in https://docs.joomla.org/Table_Basic_API_Guide

Also the Table code is in /libraries/src/Table/Table.php (post Joomla 3.8 ish) and it's worth checking if there's another php file there which reflects the Joomla table you're updating, as there could be additional functionality in the specific class which extends Joomla Table. In this case there's Module.php and so we should use the Module class in that. (Edit update: Actually, the getInstance() function called below will search the same directory to find the Module class we need, so we could call it using Table::getInstance('Module').)

To handle the update of the json string in the params field in the module record it's easiest to use the Joomla Registry (see https://github.com/joomla-framework/registry).

Finally you need to determine how you're going to identify the record you want to update. If you don't already know the id then you might need to do a database query (as described in https://docs.joomla.org/Selecting_data_using_JDatabase) to find it based on what you know.

The code below should work once you plug in the value of the record id. (Although, it doesn't check if the record is already checked out to a user for edit, which, albeit unlikely, maybe you want to check).

use Joomla\CMS\Table\Module;
use Joomla\Registry\Registry;

$id = 1;  // plug in your own value here
$m = Module::getInstance("Module");
$m->load($id);
if ($m)
{    // found the record
    $moduleParams = new Registry($m->params); // convert params string to Registry
    $currentClassSfx = $moduleParams->get("class_sfx", "");  
    $newClassSfx = $currentClassSfx . " myclass";  // or check if it's already present?
    $moduleParams->set("class_sfx", $newClassSfx);
    $newParams = (string) $moduleParams;   // convert Registry to json string
    $m->bind(array("params" => $newParams));
    $m->check();
    $m->store();
}
else
{
    // handle the case when the record isn't found
}
| improve this answer | |
  • Thank you for all your help Robbie, it's very much appreciated. – JJBW Jan 9 at 10:38

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.