I have a component that I want to use to add a module automatically.

This needs to be achieved in the administrator area.

My idea is to have a button in one of the component views. That component will make an ajax call which will then automatically create a module. I have the ajax working and running. What I want to check is adding a module directly to the database.

Is there a class that exists to do this or do I have to add it directly in to the #_modules table? and is the modules table and the assets table the only things that need updated?

The code I'm going to use to create the assets table link (which I think will work?) is:

            function setAsset($id, $table, $inherit = true){
                $parent = JTable::getInstance('Asset');

                $parentId = $parent->id;
                $name     = 'com_mycomponent.'.$table.'.'.$id;
                $title    = '';

                $asset = JTable::getInstance('Asset');

                // Check for an error.
                $error = $asset->getError();

                if ($error)
                    return false;
                    // Specify how a new or moved node asset is inserted into the tree.
                    if ($asset->parent_id != $parentId)
                        $asset->setLocation($parentId, 'last-child');

                    // Prepare the asset to be stored.
                    $asset->parent_id = $parentId;
                    $asset->name      = $name;
                    $asset->title     = $title;
                    // get the default asset rules

                    $rules = getDefaultAssetRules('com_mycomponent', $table, $inherit);
                    if ($rules instanceof JAccessRules)
                        $asset->rules = (string) $rules;

                    if (!$asset->check() || !$asset->store())
                        JFactory::getApplication()->enqueueMessage($asset->getError(), 'warning');
                        return false;
                        // Create an asset_id or heal one that is corrupted.
                        $object = new stdClass();

                        // Must be a valid primary key value.
                        $object->id = $id;
                        $object->asset_id = (int) $asset->id;

                        // Update their asset_id to link to the asset table.
                        return JFactory::getDbo()->updateObject('#__subscriptions_'.$table, $object, 'id');
                return false;
            function getDefaultAssetRules($component, $view, $inherit = true){
                // if new or inherited
                $assetId = 0;

                // Only get the actual item rules if not inheriting
                if (!$inherit)
                    // Need to find the asset id by the name of the component.
                    $db = JFactory::getDbo();
                    $query = $db->getQuery(true)
                        ->where($db->quoteName('name') . ' = ' . $db->quote($component));

                    // check that there is a value
                    if ($db->getNumRows())
                        // asset already set so use saved rules
                        $assetId = (int) $db->loadResult();
                // get asset rules
                $result =  JAccess::getAssetRules($assetId);
                if ($result instanceof JAccessRules)
                    $_result = (string) $result;
                    $_result = json_decode($_result);
                    foreach ($_result as $name => &$rule)
                        $v = explode('.', $name);
                        if ($view !== $v[0])
                            // remove since it is not part of this view
                        elseif ($inherit)
                            // clear the value since we inherit
                            $rule = array();
                    // check if there are any view values remaining
                    if (count($_result))
                        $_result = json_encode($_result);
                        $_result = array($_result);
                        // Instantiate and return the JAccessRules object for the asset rules.
                        $rules = new JAccessRules($_result);
                        // return filtered rules
                        return $rules;
                return $result;

I can't find much on line about this so I'm guessing that it's not a standard thing to do but I'm trying to automate a process for a client.

Any help thoughts would be really helpful.


  • I can offer some php refinements to this code, but I want to ask first: is this snippet already working as expected or are you needing help with a broken aspect of your task? – mickmackusa Jun 20 at 0:40
  • thanks the snippet works okay - improvements always welcome! It's more how I tackle adding a new module to the database. Is there a Joomla way of doing this or do I just create the asset id and add it to the modules table? – user1616338 Jun 20 at 9:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.