1
votes

I created a Joomla 1.7 plugin to add some custom fields to the user profile. As a basis I started with the profile plugin that is included in Joomla as example.

I added a checkboxes field like below:

<?xml version="1.0" encoding="utf-8"?>
<form>
    <fields name="profile">
        <fieldset name="profile" label="PLG_USER_WISPROFILE_SLIDER_LABEL">
            <field name="speltak1" type="checkboxes" label="PLG_USER_WISPROFILE_SPELTAK_LABEL"
                description="PLG_USER_WISPROFILE_SPELTAK_DESC">
                <option value="Bevers">PLG_USER_WISPROFILE_OPTION_BEVERS</option>
                <option value="Welpen Ochtend Horde">PLG_USER_WISPROFILE_OPTION_WELPENOCHTEND
                </option>
                <option value="Welpen Middag Horde">PLG_USER_WISPROFILE_OPTION_WELPENMIDDAG
                </option>
                <option value="Scouts Verkenners">PLG_USER_WISPROFILE_OPTION_SCOUTSVERKENNERS
                </option>
                <option value="Scouts Verkensters">PLG_USER_WISPROFILE_OPTION_SCOUTSVERKENSTERS
                </option>
                <option value="Explorers">PLG_USER_WISPROFILE_OPTION_EXPLORERS</option>
                <option value="Klimstam">PLG_USER_WISPROFILE_OPTION_KLIMSTAM</option>
            </field>
        </fieldset>
    </fields>
</form>

This will correctly add the field to the form during registration and editing the user profile. But when I try to save the profile nothing is stored in the database. The checkboxes field is converted to a array in the $data variable. But the example code can't save this to the database. Does anybody know how to do this?

2

2 Answers

1
votes

Looks like you are correctly intercepting form creation and adding your fields to the form.

You don't mention any code/functions you are running to capture the form when it is saved. Perhaps you are expecting Joomla to magically capture your new field data and save it? If so, then unfortunately it doesn't work that way. You need to do a little extra work.

Look at this page from the docs and note the 'function onUserAfterSave' portion. http://docs.joomla.org/Creating_a_profile_plugin

1
votes

The reason it is not saving is because you are passing an array element to the sql query. You need to change the form of this. Either by serializing the data or turning it into json or something. At this point, it will save in the db.

function onUserAfterSave($data, $isNew, $result, $error){
    $userId    = JArrayHelper::getValue($data, 'id', 0, 'int');

    if ($userId && $result && isset($data['gw2atpprofile']) && (count($data['gw2atpprofile']))){
        try {
            $db = &JFactory::getDbo();
            $db->setQuery('DELETE FROM #__user_profiles WHERE user_id = '.$userId.' AND profile_key LIKE \'gw2atpprofile.%\'');
            if (!$db->query()) {
                throw new Exception($db->getErrorMsg());
            }

            $tuples = array();
            $order    = 1;
            foreach ($data['gw2atpprofile'] as $k => $v) {
                if (is_array($v)){
                    $v = serialize($v);
                }
                $tuples[] = '('.$userId.', '.$db->quote('gw2atpprofile.'.$k).', '.$db->quote($v).', '.$order++.')';
            }

            $db->setQuery('INSERT INTO #__user_profiles VALUES '.implode(', ', $tuples));
            if (!$db->query()) {
                throw new Exception($db->getErrorMsg());
            }
        } catch (JException $e) {
            $this->_subject->setError($e->getMessage());
            return false;
        }
    }

    return true;
}

At that point you will need to create a way to get the information out. I know this thread is old, but I just wanted to post a reason for any new members that might stumble upon this question.