Add items to _fields and _field_groups (and _assets) during component install ?

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
david0058
Joomla! Intern
Joomla! Intern
Posts: 60
Joined: Wed Jan 20, 2016 10:58 pm

Add items to _fields and _field_groups (and _assets) during component install ?

Post by david0058 » Sun Sep 26, 2021 9:07 am

Hi,

J3.10, 4.0.2

Just fishing for ideas really (or even better, code :D ).

I built a component that uses information in the core tables where I've used fields in field groups to extend user parameters. I added all of these manually but I'd like to include this in the installation.

I could use a post-install php script, creating new entries in the _assets table and linking in the ones I create in the _fields and _fields_groups tables, though the whole nested set thing in _assets is a bit complicated. I worked it out a few years back but can't find my test code and I'd prefer not to have to dissect it again.

I searched for the Joomla install scripts, as I figured the _assets table has to be populated when Joomla is installed, but haven't found anything yet.

Any ideas or tips welcome :)

David

User avatar
Per Yngve Berg
Joomla! Master
Joomla! Master
Posts: 28197
Joined: Mon Oct 27, 2008 9:27 pm
Location: Romerike, Norway

Re: Add items to _fields and _field_groups (and _assets) during component install ?

Post by Per Yngve Berg » Sun Sep 26, 2021 9:16 am

Mod. Note: Relocated from Extensions to Developer.


https://developer.joomla.org/framework/ ... ation.html

david0058
Joomla! Intern
Joomla! Intern
Posts: 60
Joined: Wed Jan 20, 2016 10:58 pm

Re: Add items to _fields and _field_groups (and _assets) during component install ?

Post by david0058 » Mon Sep 27, 2021 5:45 pm

Thanks for re-locating, apologies for the wrong forum.

The link to the Joomla documentation is ... errm ... relevant I guess ...

Once I work out how to do this I'll see if I can post something a bit more practically useful for anyone else interested.

David

david0058
Joomla! Intern
Joomla! Intern
Posts: 60
Joined: Wed Jan 20, 2016 10:58 pm

Re: Add items to _fields and _field_groups (and _assets) during component install ?

Post by david0058 » Tue Sep 28, 2021 3:22 pm

In case anyone has a similar problem, here's some code I wrote to add new user groups. I have similar for the assets table etc.

This is all in the installer script, the functions are called from the install() and update() methods.

Code: Select all


// This is where Usergroup.php lives

JTable::addIncludePath(JPATH_LIBRARIES . '/src/Table');

// convenience class for data

class GameUserGroup
{
    public $id;
    public $title;
    public $parent_title;

    function __construct($title, $parent_title)
    {   
        $this->id = 0;
        $this->title = $title;
        $this->parent_title = $parent_title;
    }
}

And the main work is done here:

Code: Select all


...
...
...
    function install($parent)
    {   
        $this->addGameRoles();
    }
    
    function update($parent)
    {   
        $this->addGameRoles();
    }
    
    function addGameRoles()
    {
        $roles = array();
        $roles[] = new GameUserGroup('Organiser', 'Administrator');
        $roles[] = new GameUserGroup('Adviser', 'Manager');
        $roles[] = new GameUserGroup('Player', 'Registered');

        $db = JFactory::getDbo();
        $table = JTable::getInstance('Usergroup');

        foreach ($roles as $role)
            $this->addUsergroup($db, $table, $role);

        $table->rebuild();
    }

    function addUsergroup($db, $table, &$role)
    {   
        // check that the parent group exists

        $query = $db->getQuery(true);
        $query->select($db->quoteName('id'))->from($db->quoteName('#__usergroups'))->where($db->quoteName('title') . '=' . $db->quote($role->parent_title));
        $db->setQuery($query);

        try {
            $parent = $db->loadObject();

            if ($parent){
                // only add if it doesn't already exist
                $query = $db->getQuery(true);
                $query->select($db->quoteName('id'))->from($db->quoteName('#__usergroups'))->where($db->quoteName('title') . '=' . $db->quote($role->title));
                $db->setQuery($query);

                $result = $db->loadObject();

                if (!$result){      // doesn't exist so add it
                    $data = array('title' => $role->title, 'parent_id' => $parent->id, 'id' => 0);

                    if (!$table->save($data))
                        throw new Exception($table->getError());}
            }
            else {
                throw new Exception('Failed to find parent_title ' . $role->parent_title);
            }
        }
        catch (Exception $e){
            JFactory::getApplication()->enqueueMessage($e->getMessage(), 'error');
        }
    }


Post Reply

Return to “Joomla! 3.x Coding”