I'm making a custom component for Joomla CMS 2.5.x which is restricted to registered users only. On installation, and by default, the menu item returns a 500 - Server Error because the parameters haven't been set. I need to go into the Joomla Admin panel, view the Options of the component, and click on "Save"... despite the fact that I don't change anything.
How?
- Add the following line to the XML installation file:
- Now create a script.php file in the root folder of your component (ie. same as the XML installation file)
- Add the install script instructions:copyraw
// No direct access to this file defined('_JEXEC') or die; // com_XXX is your component name class com_XXXInstallerScript { function postflight($type, $parent) { // $type (install, update or discover_install) if ($type == 'install') { $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->update($db->quoteName('#__extensions')); $defaults = '{"param1":"value1","param2":"value2"}'; // JSON format for the parameters $query->set($db->quoteName('params') . ' = ' . $db->quote($defaults)); $query->where($db->quoteName('name') . ' = ' . $db->quote('com_XXX')); $db->setQuery($query); $db->query(); } } }
- // No direct access to this file
- defined('_JEXEC') or die;
- // com_XXX is your component name
- class com_XXXInstallerScript
- {
- function postflight($type, $parent)
- {
- // $type (install, update or discover_install)
- if ($type == 'install') {
- $db = JFactory::getDBO();
- $query = $db->getQuery(true);
- $query->update($db->quoteName('#__extensions'));
- $defaults = '{"param1":"value1","param2":"value2"}'; // JSON format for the parameters
- $query->set($db->quoteName('params') . ' = ' . $db->quote($defaults));
- $query->where($db->quoteName('name') . ' = ' . $db->quote('com_XXX'));
- $db->setQuery($query);
- $db->query();
- }
- }
- }
Additional: ACL during install
My component is editable by registered users only. This meant that everytime someone installs the component, they would usually need to go into the admin panel and set the ACL permissions. I want my component to work out-of-the-box:
// No direct access to this file defined('_JEXEC') or die; // com_XXX is your component name class com_XXXInstallerScript { function postflight($type, $parent) { // $type (install, update or discover_install) if ($type == 'install') { $db = JFactory::getDBO(); $query = $db->getQuery(true); $query->update($db->quoteName('#__assets')); $defaults = '{"core.admin":{"2":0},"core.manage":{"2":0},"core.create":{"2":1},"core.delete":{"2":1},"core.edit":{"2":1},"core.edit.state":{"2":1},"core.edit.own":{"2":1}}'; // JSON format for the parameters, see PHP serialize() $query->set($db->quoteName('rules') . ' = ' . $db->quote($defaults)); $query->where($db->quoteName('name') . ' = ' . $db->quote('com_XXX')); $db->setQuery($query); $db->query(); } } }
- // No direct access to this file
- defined('_JEXEC') or die;
- // com_XXX is your component name
- class com_XXXInstallerScript
- {
- function postflight($type, $parent)
- {
- // $type (install, update or discover_install)
- if ($type == 'install') {
- $db = JFactory::getDBO();
- $query = $db->getQuery(true);
- $query->update($db->quoteName('#__assets'));
- $defaults = '{"core.admin":{"2":0},"core.manage":{"2":0},"core.create":{"2":1},"core.delete":{"2":1},"core.edit":{"2":1},"core.edit.state":{"2":1},"core.edit.own":{"2":1}}'; // JSON format for the parameters, see PHP serialize()
- $query->set($db->quoteName('rules') . ' = ' . $db->quote($defaults));
- $query->where($db->quoteName('name') . ' = ' . $db->quote('com_XXX'));
- $db->setQuery($query);
- $db->query();
- }
- }
- }