1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-11 09:14:58 +02:00
This commit is contained in:
Ryan Cramer
2021-08-04 14:19:05 -04:00
parent 08c5c40705
commit c27880c71d
2 changed files with 39 additions and 0 deletions

View File

@@ -529,6 +529,25 @@ class FieldtypeOptions extends FieldtypeMulti implements Module {
$this->wire('fields')->addHookAfter('cloned', $this, 'hookCloned');
return parent::___cloneField($field);
}
/**
* Delete the given field
*
* @param Field $field Field object
* @return bool True on success, false on DB delete failure.
*
*/
public function ___deleteField(Field $field) {
$result = parent::___deleteField($field);
try {
$this->manager->deleteAllOptionsForField($field);
} catch(\Exception $e) {
$result = false;
$this->error($e->getMessage());
$this->trackException($e);
}
return $result;
}
/**
* Hook called when field is cloned, to clone the selectable options from old field to new field

View File

@@ -669,6 +669,26 @@ class SelectableOptionManager extends Wire {
return $cnt;
}
/**
* Delete all options for given field
*
* @param Field $field
* @return int
* @throws WireException
*
*/
public function deleteAllOptionsForField(Field $field) {
$database = $this->wire()->database;
$table = self::optionsTable;
$sql = "DELETE FROM `$table` WHERE fields_id=:fields_id";
$query = $database->prepare($sql);
$query->bindValue(':fields_id', $field->id, \PDO::PARAM_INT);
$query->execute();
$cnt = $query->rowCount();
$this->message("Deleted $cnt row(s) from table $table", Notice::debug);
return $cnt;
}
/**
* Add the given option titles for $field
*