Jump to content

Correctly deleting fields from unknown templates


Recommended Posts

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

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.

  • Like 1
Link to comment
Share on other sites

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);
    }
}
  • Like 2
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...