GuruMeditation Posted April 11, 2016 Share Posted April 11, 2016 Hi all, I'm a bit confused with the following issue. Let's say my module installs a couple of fields (see below) for future use, i.e they are not attached to any templates on install, but the user can add the fields to any templates they want after install. if(!$this->fields->get('test')) { $field_test = new Field(); $field_test->type = $this->modules->get("FieldtypeText"); $field_test->name = 'test'; $field_test->label = 'Test field'; $field_test->save(); } if(!$this->fields->get('test2')) { $field_test2 = new Field(); $field_test2->type = $this->modules->get("FieldtypeText"); $field_test2->name = 'test2'; $field_test2->label = 'Test field 2'; $field_test2->save(); } How exactly do I use the uninstall method to correctly remove them without errors if they have been attached to various templates? I didn't create a fieldgroup for them as they can be independently installed to different templates, so a fieldgroup didn't make sense to me as they are independent fields and not part of a group. But if I use the following code for the uninstall method and they are connected to a template, I get an error and the module can no longer be uninstalled (I have to manually delete the leftovers from the database). // Array of fields to delete $fs = array( 'test', 'test2', ); // Delete the fields $fields = wire('fields'); foreach ($fs as $f) { $fields->delete($fields->get($f)); } Any tips or advice? Cheers Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 12, 2016 Share Posted April 12, 2016 http://cheatsheet.processwire.com/fields/field-methods/field-getfieldgroups/ This will give you all fieldgroups (aka templates) you need to remove the field from before deleting it. 4 Link to comment Share on other sites More sharing options...
GuruMeditation Posted April 12, 2016 Author Share Posted April 12, 2016 Thanks, that makes sense. What would happen if my module deleted the fieldgroup, but other unknown fields were also linked to that fieldgroup? Is it best to actually add my fields to a unique fieldgroup to avoid any conflict with other fields etc? Link to comment Share on other sites More sharing options...
LostKobrakai Posted April 12, 2016 Share Posted April 12, 2016 I think you're missunderstanding fieldgroups. Fieldgroups hold the fields for templates. Each template does have it's own fieldgroup. You just need to remove your field from the fieldgroup. Do not delete the whole fieldgroup or the template of it won't have fields anymore. 1 Link to comment Share on other sites More sharing options...
GuruMeditation Posted April 12, 2016 Author Share Posted April 12, 2016 Ok, I think I get it now. I knocked up the following code which I'll post here in case anyone else finds it useful. It seems to work ok public function ___uninstall() { $fields = wire('fields'); // Array of fields to delete $fs = array( 'test', 'test2', ); foreach ($fs as $f) { // Get each field from the array above $f = $fields->get($f); // Get the fieldgroups for each field $fgs = $f->getFieldgroups(); // Loop through each fieldgroup foreach ($fgs as $fg) { // Remove the field from each fieldgroup it's linked to $fg->remove($f); $fg->save(); } // Now delete the field $fields->delete($f); } } 2 Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now