1

I'm making a plugin that stores elements in fields and fieldGroups. When deleting one the elements with the fields/field groups are being removed too.

But when deleting the whole plugin, those fields are still in the database. When reinstalling the plugin this makes it impossible to add the field with the same title again.

I've already added 'onDelete' things in all the records like this:

public function defineRelations()
{
  return array(
    'form'        => array(static::BELONGS_TO, 'PluginHandle_FormRecord', 'onDelete' => static::CASCADE),
    'fieldGroup'  => array(static::BELONGS_TO, 'FieldGroupRecord', 'onDelete' => static::CASCADE),
    'field'       => array(static::BELONGS_TO, 'FieldRecord', 'onDelete' => static::CASCADE)
  );
}

I've been searching the docs to see what actually happens when a plugin is being deleted, but can't find anything. Any ideas?

2
  • You're assuming that someone hasn't added extra fields to the group you want to delete? Feb 3 '17 at 10:36
  • Am I? The base of the plugin is a formbuilder. When someone uninstalls the plugin, I want to delete all the created forms (and it's belongings) with it, so when the person reinstalls there won't be any conflicts w/ existing fields and fieldgroups in the database.
    – sanderfish
    Feb 3 '17 at 10:47
1

You can see what Craft does when uninstalling a plugin by looking at craft/app/services/PluginsService's uninstallPlugin method.

Craft will delete any custom elements the plugin has defined from craft_elements and the cascading deletes across foreign keys to that table will trigger. It will drop any tables defined by any plugin records. It will remove the plugin row from the craft_plugins table and any migrations from the craft_migrations table.

craft_field and craft_fieldgroups won't get picked up because they don't have any indirect FK connection to craft_elements, but you can add an onBeforeUninstall method in your base plugin class and do any custom cleanup you want there.

1
  • Thanks for your reply. You mention craft_field and craft_fieldgroups won't get picked up. How would someone handle those when writing a plugin? Currently when a plugin is uninstalled and reinstalled, the same name can't be used. I don't think this is great behaviour. Do you have any suggestions on improving this on my side?
    – sanderfish
    Feb 3 '17 at 9:54

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.