Jump to content

Rename Repeater field, including template and fieldgroup


Robin S
 Share

Recommended Posts

I discovered that when you rename a Repeater or RepeaterMatrix field, the template and fieldgroup used for the Repeater items keep the old name. So I wrote a little function for renaming a Repeater field including the associated template and fieldgroup.

To be clear, you do not need to use this function to rename a Repeater field in normal circumstances. The Repeater template is a system template so it's not something you see normally, and as for the fieldgroup... what on earth is a fieldgroup, right? :)

So this is just for perfectionists. Or if you are doing something like checking the template name in some hook and you don't want to have to deal with the old field name.

/**
 * Rename a Repeater / RepeaterMatrix field, including template and fieldgroup
 * @param string $old_name The existing name of the Repeater field
 * @param string $new_name The new name for the Repeater field
 */
function renameRepeaterField($old_name = '', $new_name = '') {
    // Both arguments are required
    if(!$old_name || !$new_name) {
        wire('log')->error('renameRepeaterField(): Both $old_name and $new_name arguments are required.');
        return;
    }
    
    // Rename repeater field
    $f = wire('fields')->get($old_name);
    if(!$f) {
        wire('log')->error("renameRepeaterField(): Field '$old_name' not found.");
        return;
    };
    if(!$f->type instanceof FieldtypeRepeater) {
        wire('log')->error("renameRepeaterField(): Field '$old_name' is not an instance of FieldtypeRepeater.");
        return;
    }
    $f->name = $new_name;
    $label = str_replace('_', ' ', ucfirst($new_name));
    $f->label = $label;
    $f->save();
    
    // Rename template
    $t = wire('templates')->get('repeater_' . $old_name);
    $t->flags = Template::flagSystemOverride;
    $t->flags = 0;
    $t->save();
    $t->name = 'repeater_' . $new_name;
    $t->flags = 8;
    $t->save();

    // Rename fieldgroup
    $fg = wire('fieldgroups')->get('repeater_' . $old_name);
    $fg->name = 'repeater_' . $new_name;
    $fg->save();
}

// Use the function in a one-off operation like this
renameRepeaterField('sucky_old_name', 'shiny_new_name');

The function derives a label for the field by replacing underscores with spaces and capitalising the first letter of the field name. If you want a different label you can just edit the field in admin afterwards.

  • Like 3
Link to comment
Share on other sites

Hey @Robin S - I can't find it, but I am pretty sure I posted about this issue quite a while ago. I think this is critical and should be fixed in the core. I am sure there are more than a few modules that rely on a "repeater_{$field->name}" pattern for getting the template. They probably shouldn't, but it's also just one of those inconsistencies that shed a less than stellar light on PW (in my humble opinion).

BTW, templates can use alternate fieldgroups - not sure how often this happens in practice, but that is the origin of that separation.

  • Like 2
Link to comment
Share on other sites

22 hours ago, adrian said:

I think this is critical and should be fixed in the core.

You're right; I created a new issue for this: https://github.com/processwire/processwire-issues/issues/395

22 hours ago, adrian said:

BTW, templates can use alternate fieldgroups - not sure how often this happens in practice, but that is the origin of that separation.

I know, I was just kidding a bit because I think for most people fieldgroups are one of the more arcane aspects of PW.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...