From db88618e2d7d15a3992f8512ad0c3b2ab8824878 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 17 Aug 2023 10:28:18 -0400 Subject: [PATCH] Fix issue processwire/processwire-issues#1730 Co-authored-by: matjazpotocnik --- .../FieldtypeFile/FieldtypeFile.module | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module b/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module index c7bd05ce..e95294c5 100644 --- a/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module +++ b/wire/modules/Fieldtype/FieldtypeFile/FieldtypeFile.module @@ -1328,6 +1328,32 @@ class FieldtypeFile extends FieldtypeMulti implements ConfigurableModule, Fieldt return parent::___deleteField($field); } + /** + * Return a cloned copy of $field + * + * @param Field $field + * @return Field cloned copy + * + */ + public function ___cloneField(Field $field) { + $this->wire()->fields->addHookAfter('cloned', function(HookEvent $e) { + $e->removeHook(null); // run once only + $oldField = $e->arguments(0); /** @var Field $oldField */ + $newField = $e->arguments(1); /** @var Field $newField */ + if(!$newField->type instanceof FieldtypeFile) return; + $templates = $e->wire()->templates; + $oldTpl = $templates->get("field-$oldField|field-x-$oldField"); + if(!$oldTpl) return; // no custom field-* template present + $newTpl = $templates->clone($oldTpl, str_replace("$oldField", "$newField", $oldTpl->name)); + if($newTpl) { + $e->message(sprintf(__('Cloned fields template - %1$s => %2$s'), $oldTpl->name, $newTpl->name)); + } else { + $e->warning(sprintf(__('Error cloning template - %1$s => %2$s'), $oldTpl->name, $newTpl->name)); + } + }); + return parent::___cloneField($field); + } + /** * Default list of file extensions supported by this field, and used as the default by getConfigInputfields() * method.