Jump to content

removing fields from fieldgroups


benbyf
 Share

Recommended Posts

Never really got my head around field groups and it shows, In my module uninstall I think I figured out how to delete a page, then a template and it's fieldgroup so that nothing gets upset. But for fields that belong to templates that need to stoick around I'm still abit stuck to how you would remove the field and when I try I still get:

Unable to delete field '[field name]' because it is in use by these fieldgroups: 'user'

is there a tutorial or best method for removing added fields, pages and templates?

Link to comment
Share on other sites

ended up with this, but be nice to get a best practice of tutorial on this as still don't really understand.

	public function ___uninstall() {
		// for each installed page delete on uninstall
		$pages = array('my_test_page');
		foreach ($pages as $key => $value) {
			$page = $this->pages->get("name=$value, include=all");
			$this->wire('log')->save('mm_log', $page->id);
			if($page->id){
				
				// delete page and any children
				$page->delete(true);
				$page->trash();
			}
		}

		// delete field groups used on system templates
		$f = $this->fields->get('my_repeater');
		$userTemplate = $this->templates->get("user");
		if($userTemplate->id) {

			// $userTemplate = $this->templates->get("user");
			
			// $fg = $userTemplate->fieldgroup->remove($f);
			// $fg->save();

			$userTemplate->fields->remove($f);
            $userTemplate->fields->save();

			$this->wire('log')->save('mm_log', 'delete user field');
		}

		// delete messages template
		$templates = array('my_template');
		foreach ($templates as $key => $value) {

			$t = $this->templates->get($value);

			if($t->id){
				$t->flags = Template::flagSystemOverride; 
				$t->flags = 0; 
				$this->templates->delete($t);
			}
			$fieldgroup = $this->fieldgroups->get($value);
			if($fieldgroup){
				$this->fieldgroups->delete($fieldgroup);
			}
		}

		// for each installed field delete on uninstall
		$fields = array('my_field');

		foreach ($fields as $value) {
			$f = $this->fields->get($value);
			if($f->id){
				// if field exists, then delete
				$this->fields->delete($f);
			}
		}
	
		$this->modules->uninstall($moduleName);
	}

 

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...