Jump to content

How do I delete a repeater field via API


hellomoto
 Share

Recommended Posts

I have a module that creates a repeater field on install:


        $f = new Field();
        $f->type = wire('modules')->get('FieldtypeFieldsetTabOpen');
        $f->set("name", "iu_maptab")->set("label", "Mapping");
        $f->set("tags", "impupd")->save();
        $r = new Field(); $rName = "iu_map";
        $r->type = wire('modules')->get('FieldtypeRepeater');
        $r->set("name", $rName)->set("label", "Map Fields");
        $r->set("tags", "impupd")->save();
        $f = new Field();
        $f->type = wire('modules')->get('FieldtypeSelectExtOption');
        $f->set("name", "iu_field")->set("label", "Template Field");
        $f->option_table = "fields";
        $f->option_value = "id"; $f->option_label = "name";
        $f->set("tags", "impupd")->set("columnWidth",50)->save();
        $f = new Field();
        $f->type = wire('modules')->get('FieldtypeText');
        $f->set("name", "iu_value")->set("label", "Source Value Selector");
        $f->set("tags", "impupd")->set("columnWidth",50)->save();
        $f = new Field();
        $f->type = wire('modules')->get('FieldtypeCheckbox');
        $f->set("name", "iu_static")->set("label", "Use Static Value");
        $f->set("tags", "impupd")->set("columnWidth",50)->save();
        $f = new Field();
        $f->type = wire('modules')->get('FieldtypeFieldsetClose');
        $f->set("name", "iu_maptab_close");//->set("label", "Mapping");
        $f->set("tags", "impupd")->save();
        $rfg = new Fieldgroup();
        $rfg->name = "repeater_{$rName}";
        $rFields = ["iu_field", "iu_value", "iu_static"];
        foreach($rFields as $rf) {
          $rfg->append($this->fields->get($rf));
        }
        $rfg->save();
    		$rt = new Template();
    		$rt->name = "repeater_$rName";
    		$rt->flags = 8;
    		$rt->noChildren = 1;
    		$rt->noParents = 1;
    		$rt->noGlobal = 1;
    		$rt->slashUrls = 1;
    		$rt->fieldgroup = $rfg;
        $rt->save();
        $rpg = "for-field-{$r->id}";
		    $r->parent_id = $this->pages->get("name=$rpg")->id;
		    $r->template_id = $rt->id;
		    $r->repeaterReadyItems = 3;
        foreach($rfields as $rf) {
          $r->repeaterFields = $this->fields->get($rf);
        }
        $r->save();

Now I can't uninstall it, on account of a field being used in the fieldgroup created for the repeater -- can't delete that field because it's in the fieldgroup which I am trying and failing to delete:


        $rf = wire('fields')->get('iu_map');
        $rt = wire('templates')->get('repeater_iu_map');
        $rfg = $rt->fieldgroup;
        if($rf) {
          wire('fields')->delete($rf);
          wire('fieldgroups')->delete($rfg);
          wire('templates')->delete($rt);
        }
        foreach(wire('fields')->find("tags*=impupd") as $f) {
          wire('fields')->delete($f);
        }
Quote

ProcessModule: Unable to delete field 'iu_field' because it is in use by these fieldgroups: 'repeater_iu_map'

Please help this is such a bother. The fieldgroup should be deleted before attempting to delete the field itself.

Link to comment
Share on other sites

System fields and templates cannot be deleted without first removing the 'system flag'. Example code:

$t = $this->wire('templates')->get('your-template');
if ($t->id) {

    // two step process to delete system template
    if ($t->flags == 8) {
         $t->flags = Template::flagSystemOverride;
         $t->flags = 0;
         $this->wire('templates')->delete($t);
         // delete the associated fieldgroups
         $this->wire('fieldgroups')->delete($t->fieldgroup);
    }

}

 

  • Like 1
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

×
×
  • Create New...