1

I have a system plugin where I need to change some existing field name values in xml manifest file. In a new plugin installation this is not a problem, but if I want to upgrade an older version I will have a problem because fields names does not match and it will lose the older params values. Is there a way, maybe with a installation script, to reassign the new fields names to the older ones?

3

Yes, you are right.

You can use installation script to update the field name and value on the plugin upgrade.

Write sql to update the plugin data, stored in the database, as per the current plugin field name value.

Put your code in the update method of the installation script so that it will not be execute on fresh installation.

  • Could you please take a look at code and tell me if there is a more appropriate way of doing it? – n.h. Apr 3 '15 at 16:25
  • code seems fine – Nick Apr 4 '15 at 8:24
1

Based on Nick's answer I tried this solution with success:

public function update(JAdapterInstance $adapter)
{
    $version = self::getXMLVersion();

    if (version_compare($version, 'my_plugin_version', '<') == 1) 
    {
        $db = JFactory::getDbo();

        $query = $db->getQuery(true);

        $fieldsToGet = array(
            $db->quoteName('params')
        );

        $conditions = array(
            $db->quoteName('element') . ' = ' . $db->quote('my_plugin_name'), 
            $db->quoteName('folder') . ' = ' . $db->quote('system')
        );

        $query->select($fieldsToGet)->from($db->quoteName('#__extensions'))->where($conditions);
        $db->setQuery($query);   
        $results = $db->loadResult(); 

        $options = json_decode($results, true);

        // Array with all replacements
        $new = array(
        'old_name_01'           => 'new_name_01',
        'old_name_...'          => 'new_name_...',
        'old_name_99'           => 'new_name_99');

        // Replace fields
        foreach ($options as $oldKey => $value) 
        {
            $newKey = $new[$oldKey];
            $newArray[$newKey] = $value;
        }

        $results = json_encode($newArray);

        $fieldsToUpdate = array(
            $db->quoteName('params') . ' = ' . $db->quote($results)
        );

        $query->update($db->quoteName('#__extensions'))->set($fieldsToUpdate)->where($conditions);
        $db->setQuery($query);
        $result = $db->execute();
    }
    else
    {
        return;
    }

}

private function getXMLVersion()
{   
    $version = '';

    $path = JPATH_PLUGINS . '/system/my_plugin_name/my_plugin_name.xml';

    $xml = JApplicationHelper::parseXMLInstallFile($path);

    if ($xml && isset($xml['version']))
    {
        $version = trim($xml['version']);
    }

    return $version;
}
  • 1
    if ($xml && isset($xml['version'])) can be more simply written as if (isset($xml['version'])). Or 3v4l.org/jVmZl – mickmackusa Mar 26 at 1:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.