2

Has anyone written an upgrade for a fieldtype? I'm assuming so, but I've not actually ventured this direction, and there are zero docs in this arena.

Specifically in this case, upgrading a fieldtype with no settings to have per-field settings, which will update the fieldtype preferences in the DB etc etc.

Direction? Hacky style required?

3 Answers 3

1

Well, it's something difficult for me to write everything here related to field type update :). let me try to give you some pointer.

While creating a field type, you were creating a $info array having "name" and "version". If you are going to update you field type, first step will be to higher the "version" value within the $info array.

Second thing: the field type file will have a function update like:

function update($from)
{
if($from < 2.0){
[process the stuffs which you need to update in the updated version of 2.0 in the case your previous version was less than 2.0]
}
}

Now it's depend on you, what you would like to update. If you would like to update the settings or field type display then you will need to modify the code within their function. In the update function you can re-structure database related stuffs.

I hope, my little pointer will help you.

Best Regards,

1
  • So the ft.whatever.php supports update()? Since this stuff is serialized, are there helpers somewhere to push this data around or am i on my own? I can write whatever, but I don't want to reinvent the wheel here. Feb 12, 2013 at 17:41
1

Although it isn't documented BKY is right, fieldtypes can have update() methods. Here's the relevant bit of code from addons_fieldtypes.php:

// Update if version changed
$version = $installed[$ft]['version'];

if ($FT->info['version'] > $version && method_exists($FT, 'update') && $FT->update($version) !== FALSE)
{
    $this->db->update('fieldtypes', array('version' => $FT->info['version']), array('name' => $ft));
}

As with other update() methods you should return TRUE if the update was a success and FALSE if it's failed.

function update($from)
{
    if ($from == MY_ADDON_CURRENT_VERSION) return FALSE;

    if (version_compare($from, '1.1', '<'))
    {
        // Update Stuff
    }

    return TRUE;
}

I'd be happy to be proven wrong but don't think there are any helpers for retrieving/updating FT settings. You'd need to do all of the querying/serializing/base64 encoding* yourself.

* Why, EllisLab, why?

2
  • When is this update function actually called? Sep 25, 2013 at 22:03
  • As far as I can see, only when the fieldtype settings page is loaded.
    – Dom Stubbs
    Sep 26, 2013 at 9:38
1

Turns out the update method is available, BUT it's only triggered if you visit the global settings for the fieldtype.

Unfortunately, if you create a feildtype which doesn't have global settings, you're kinda boned and will have to find a workaround for querying/updating the installed version in the database and setting 'has_global_settings' to 'y' in exp_fieldtypes.

Such a PITA.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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