From c27880c71d2e15c2d911c2bb77db11122cfd9530 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Wed, 4 Aug 2021 14:19:05 -0400 Subject: [PATCH] Fix issue processwire/processwire-issues#1172 --- .../FieldtypeOptions/FieldtypeOptions.module | 19 ++++++++++++++++++ .../SelectableOptionManager.php | 20 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module b/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module index 198e2e41..d6aa17aa 100644 --- a/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module +++ b/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module @@ -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 diff --git a/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionManager.php b/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionManager.php index ae36ba16..51f29b79 100644 --- a/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionManager.php +++ b/wire/modules/Fieldtype/FieldtypeOptions/SelectableOptionManager.php @@ -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 *