URBANZ Posted April 17, 2020 Share Posted April 17, 2020 (edited) So i ran in to an issue unless i've missed something. As the methods are pulled from the module.json file to display the tabs in admin panel i wanted to dynamically add another if a user activates a feature. I thought i would post this if any other people have this issue. $array = array( //"hide" => true, //can also add hide element if needed "text" => "TEXT HERE", "method" => "METHODNAME" ); $json = file_get_contents('modules/installed/MODULEHERE/module.json'); $decoded = json_decode($json); $decoded->admin[] = $array; file_put_contents('modules/installed/MODULEHERE/module.json', json_encode($decoded)); I haven't tested this much but it is currently functioning as desired in one of my modules. I'm sure this probably could be done better/another way. Let me know your thoughts. This is full usage including removing the array when deactivated again. if (isset($this->methodData->SETTINGSVALUE)) { $settings->update("SETTINGSVALUE", $this->methodData->SETTINGSVALUE); $json = file_get_contents('modules/installed/YOURMODULE/module.json'); $decoded = json_decode($json); if($settings->loadSetting("SETTINGSVALUE")){ $array = array( "text" => "YOUR TEXT", "method" => "METHOD" ); $decoded->admin[] = $array; } else { /* This has -1 as starts 1 item from the last then counts 1 up and removes it can change to how many items you want. makes it much easier if you need to add multiple items then remove them later. just make sure you are removing the same amount you added or you will remove other arrays. */ array_splice($decoded->admin, -1, 1); } file_put_contents('modules/installed/YOURMODULE/module.json', json_encode($decoded)); header("Location: /?page=admin&module=YOURMODULE"); } Edited April 17, 2020 by urbanmafia Quote Link to comment Share on other sites More sharing options...
Magictallguy Posted April 17, 2020 Share Posted April 17, 2020 Nice! Quick tip; appending an array is cheaper on the resources (and therefore faster) than array_push(). Recommendation: Alter array_push($decoded->admin, $array); to $decoded->admin[] = $array; 1 Quote Link to comment Share on other sites More sharing options...
URBANZ Posted April 17, 2020 Author Share Posted April 17, 2020 7 minutes ago, Magictallguy said: Nice! Quick tip; appending an array is cheaper on the resources (and therefore faster) than array_push(). Recommendation: Alter array_push($decoded->admin, $array); array_push($decoded->admin, $array); to $decoded->admin[] = $array; $decoded->admin[] = $array; Thankyou MTG much better way. updated in first post if someone uses it. Quote Link to comment Share on other sites More sharing options...
Dayo Posted April 17, 2020 Share Posted April 17, 2020 (edited) /* This hook is run before any module is loaded */ new Hook("moduleLoad", function ($loadedModule) { global $page; /* Only make this change if the user is on the admin page */ if ($loadedModule == "admin") { /* Insert new admin menu link */ $page->modules["crimes"]["admin"][] = array( "text" => "test", "method" => "edit" ); } /* Always return the $loadedModule as this function can be used to change the module to load i.e. if ($loadedModule == "jail") { return "prison"; } return $loadedModule; */ return $loadedModule; }); Thius is a way to do it with hooks and bypassing file permission errors Edited April 17, 2020 by Dayo Quote Link to comment Share on other sites More sharing options...
URBANZ Posted April 17, 2020 Author Share Posted April 17, 2020 1 minute ago, Dayo said: /* This hook is run before any module is loaded */ new Hook("moduleLoad", function ($loadedModule) { global $page; /* Only make this change if the user is on the admin page */ if ($loadedModule == "admin") { /* Insert new admin menu link */ $page->modules["crimes"]["admin"][] = array( "text" => "test", "method" => "edit" ); } /* Always return the $loadedModule as this function can be used to change the module to load i.e. if ($loadedModule == "jail") { return "prison"; } return $loadedModule; */ return $loadedModule; }); /* This hook is run before any module is loaded */ new Hook("moduleLoad", function ($loadedModule) { global $page; /* Only make this change if the user is on the admin page */ if ($loadedModule == "admin") { /* Insert new admin menu link */ $page->modules["crimes"]["admin"][] = array( "text" => "test", "method" => "edit" ); } /* Always return the $loadedModule as this function can be used to change the module to load i.e. if ($loadedModule == "jail") { return "prison"; } return $loadedModule; */ return $loadedModule; }); Thius is a way to do it with hooks and bypassing file permission errors Thank you didn't know this existed tbh still getting my head around alot of the hooks Quote Link to comment Share on other sites More sharing options...
Dayo Posted April 17, 2020 Share Posted April 17, 2020 Yeah i need to sit down and document them all there are loads that people don't know of 1 Quote Link to comment Share on other sites More sharing options...
Sim Posted April 17, 2020 Share Posted April 17, 2020 2 hours ago, Dayo said: Yeah i need to sit down and document them all there are loads that people don't know of Even if you don't want to document them all. Just supplying us with a list of them would be a good start, us users can document them of we get bored. 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.