From 7d3dc807984b4bb77595acc4e8eaaf8af1483901 Mon Sep 17 00:00:00 2001 From: Ryan Cramer Date: Thu, 12 Oct 2017 10:36:53 -0400 Subject: [PATCH] Fix issue processwire/processwire-issues#395 where renaming a repeater field didn't rename the related template and fieldgroup --- .../FieldtypeRepeater.module | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module b/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module index ee90b3b8..68d96f54 100644 --- a/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module +++ b/wire/modules/Fieldtype/FieldtypeRepeater/FieldtypeRepeater.module @@ -1042,6 +1042,32 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule { return $parent; } + /** + * Update repeater template and fieldgroup to have same name as field + * + * @param Template $template Template having old name + * @param string $name New name for template + * + */ + protected function updateRepeaterTemplateName(Template $template, $name) { + + if($template->name != $name && !$this->wire('templates')->get($name)) { + $this->message("Renamed repeater template from '$template->name' to '$name'", Notice::debug); + $flags = $template->flags; + $template->flags = Template::flagSystemOverride; + $template->flags = 0; + $template->save(); + $template->name = $name; + $template->flags = $flags; + $template->save(); + } + + if($template->fieldgroup && $template->fieldgroup->name != $name && !$this->wire('fieldgroups')->get($name)) { + $template->fieldgroup->name = $name; + $template->fieldgroup->save(); + } + } + /** * Return the repeater template used by Field, i.e. repeater_name * @@ -1056,11 +1082,18 @@ class FieldtypeRepeater extends Fieldtype implements ConfigurableModule { $template = null; $templateID = (int) $field->get('template_id'); - if($templateID) $template = $this->wire('templates')->get($templateID); - if($template) return $template; - - // template does not exist, so we create it $templateName = self::templateNamePrefix . $field->name; + + if($templateID) { + $template = $this->wire('templates')->get($templateID); + if($template && $template->name !== $templateName) { + // repeater has been renamed, update the template and fieldgroup names + $this->updateRepeaterTemplateName($template, $templateName); + } + } + + // if template already exists, return it now + if($template) return $template; // make sure the template name isn't already in use, make a unique one if it is $n = 0;