Jump to content

Delete FieltypeFile field


Richard Jedlička
 Share

Recommended Posts

Hi,

I'm just trying to delete file field via API. I want to delete it completely without concerning which pages is using it. I found this code:

$fields = wire('fields');
foreach ($fields->find('name=field_name') as $field) {
	foreach ($field->getFieldgroups() as $fieldgroup) {
		$fieldgroup->remove($field);
		$fieldgroup->save();
	}

	$fields->delete($field);
}

This removes the field from database it is OK. But it doesn't remove the files (from filesystem) that ware added to pages.

As I was going through PW source code I've found that I have to save the page also, but it really complicates the thing. The problem starts in FieldtypeFile::___deletePageField method where the $pagefile is handled differently according to field output formatting. If the $pagefile is PageFile, the unlink is called which also deletes the file from filesystem. If the $pagefile is PageFiles the deleteAll is called which only adds hooks on page save. Why is it done this way? This makes inconsistency when deleting the field. Is there some better way to delete the field without caring about pages?

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