Joomla Component: Set default params on installation

What?
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?
  1. Add the following line to the XML installation file:
    copyraw
    <scriptfile>script.php</scriptfile>
    1.  <scriptfile>script.php</scriptfile> 
  2. Now create a script.php file in the root folder of your component (ie. same as the XML installation file)
  3. 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();
                    }
            }
    }
    1.  // No direct access to this file 
    2.  defined('_JEXEC') or die; 
    3.   
    4.   // com_XXX is your component name 
    5.  class com_XXXInstallerScript 
    6.  { 
    7.          function postflight($type, $parent) 
    8.          { 
    9.                  // $type (install, update or discover_install) 
    10.                  if ($type == 'install') { 
    11.                          $db = JFactory::getDBO()
    12.                          $query = $db->getQuery(true)
    13.                          $query->update($db->quoteName('#__extensions'))
    14.                          $defaults = '{"param1":"value1","param2":"value2"}'// JSON format for the parameters 
    15.                          $query->set($db->quoteName('params') . ' = ' . $db->quote($defaults))
    16.                          $query->where($db->quoteName('name') . ' = ' . $db->quote('com_XXX'))
    17.                          $db->setQuery($query)
    18.                          $db->query()
    19.                  } 
    20.          } 
    21.  } 

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:
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('#__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();
                }
        }
}
  1.  // No direct access to this file 
  2.  defined('_JEXEC') or die; 
  3.   
  4.   // com_XXX is your component name 
  5.  class com_XXXInstallerScript 
  6.  { 
  7.          function postflight($type, $parent) 
  8.          { 
  9.                  // $type (install, update or discover_install) 
  10.                  if ($type == 'install') { 
  11.                          $db = JFactory::getDBO()
  12.                          $query = $db->getQuery(true)
  13.                          $query->update($db->quoteName('#__assets'))
  14.                          $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() 
  15.                          $query->set($db->quoteName('rules') . ' = ' . $db->quote($defaults))
  16.                          $query->where($db->quoteName('name') . ' = ' . $db->quote('com_XXX'))
  17.                          $db->setQuery($query)
  18.                          $db->query()
  19.                  } 
  20.          } 
  21.  } 
Category: Joomla :: Article: 529

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.