From 5554e87b4732729926b274d4111b4eec3a559a0d Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Mon, 13 Aug 2018 05:56:57 -0400 Subject: [PATCH] Fix issue processwire/processwire-issues#667 where cloning a FieldtypeOptions field was only cloning the field, and not the options. --- .../FieldtypeOptions/FieldtypeOptions.module | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module b/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module index 5d80adf6..a6a3fe5e 100644 --- a/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module +++ b/wire/modules/Fieldtype/FieldtypeOptions/FieldtypeOptions.module @@ -419,6 +419,40 @@ class FieldtypeOptions extends FieldtypeMulti implements Module { $data['errors']['export_options'] = $this->_('Import of options is not yet implemented. Though they can easily be imported from one site to another by copy/paste directly from the field edit screen.'); return $data; } + + /** + * Return a cloned copy of $field + * + * @param Field $field + * @return Field cloned copy + * + */ + public function ___cloneField(Field $field) { + $this->wire('fields')->addHookAfter('cloned', $this, 'hookCloned'); + return parent::___cloneField($field); + } + + /** + * Hook called when field is cloned, to clone the selectable options from old field to new field + * + * #pw-internal + * + * @param HookEvent $event + * + */ + public function hookCloned(HookEvent $event) { + /** @var Field $oldField */ + $oldField = $event->arguments(0); + /** @var Field $newField */ + $newField = $event->arguments(1); + /** @var FieldtypeOptions $fieldtype */ + $fieldtype = $oldField->type; + if(!$fieldtype instanceof FieldtypeOptions) return; + $options = $fieldtype->getOptions($oldField); + $options->setField($newField); + $fieldtype->addOptions($newField, $options); + $event->removeHook(null); + } /** * Get Inputfields needed to configure this Fieldtype