Jump to content

Page Rename Options


adrian

Recommended Posts

42 minutes ago, adrian said:

If you do feel like debugging, you'll see that the way I handle renaming when the settings is hidden is to move the name field to the Content tab and then hide it

Thx for the info. That sounds strange - it should indeed work. As mentioned I've already moved along and changed my setup, so it's not easy to reproduce this again I'm afraid. If it does not affect others it should be fine ? 

Link to comment
Share on other sites

  • 2 years later...

Hi @adrian,

My standard settings when using this module are "Automatically rename the name field to match the title" and "Initial differences protected" and this generally works great for me - the title and page name are always kept in sync except when I have deliberately changed the name to something that I don't want automatically changed.

But one case that has been catching me out is when a page is cloned using the core ProcessPageClone module. When this is done the clone gets a title in the format "Original page name (copy)" but the page name gets the format "original-page-name-1". So from that point on the page gets treated as a case of "initial differences protected" and therefore the name doesn't get updated when the title is changed.

Do you think it would be a worthwhile addition to Page Rename Options to try and detect this scenario somehow so that it doesn't count as a case of "initial differences protected"? I'm not sure what the best way to do this would be, but one approach would be to hook after ProcessPageClone has executed and set the page name from the title (e.g. "original-page-name-copy") so that it doesn't count as an initial difference.

Update: one more thing...

I reckon it would be cool if the module added a button to the name inputfield in the Settings tab which when clicked would sync the name to the current title (via JS I guess). 

Link to comment
Share on other sites

  • 2 weeks later...

Hi @Robin S - sorry for the really late response.

Looks like your suggestion for matching the cloned name to its title works for me. Would you mind testing at your end please. Just add this at the start of the ready method.

// if cloning a page, then set the name to match the title so Protect Initial Differences works
$this->wire()->addHookAfter("Pages::cloned", function($event) {
    $p = $event->arguments(1);
    $p->setAndSave('name', $this->wire('sanitizer')->pageName($p->title, Sanitizer::translate));
});

This approach also works when the copy button allows one step ajax cloning.

I'll look into your sync button idea as well.

  • Like 1
Link to comment
Share on other sites

3 hours ago, adrian said:

Would you mind testing at your end please.

Yes, it works great, thanks.

If you clone a page more than once then after the first clone the later ones will still count as "initial differences" because each clone gets a title in the format "Original page name (copy)" but the cloned pages can't all have a name that matches this title under the same parent. And changing the automatic titles of the cloned pages is perhaps going too far, so I think the hook you suggested is sufficient for the basic case.

Thanks again!

Link to comment
Share on other sites

What about if the module were to just disable the Initial Differences Protected check if the name doesn't match the title but the title contains (copy\s?\d?\) which would match (copy), (copy 2), etc. I think this approach will work better. Thoughts?

Link to comment
Share on other sites

Try this as a replacement for https://github.com/adrianbj/PageRenameOptions/blob/8665aca5023417163841603fda8037df2ccbe245/PageRenameOptions.module#L106-L108

Not well tested yet, but wanted to give you a better idea of what I meant.

        preg_match('/\(copy\s?\d?\)/', $p->title, $matches);
        $isTitleCloned = !empty($matches) && $p->name != $this->wire('sanitizer')->pageName($p->title, Sanitizer::translate);
        if(!$isTitleCloned && $this->data['initialDifferencesProtected'] && !$this->wire('user')->roles->has("name=".implode("|", $this->data['exemptRolesInitialDifferencesProtected']))) {
            $this->wire('config')->scripts->add($this->wire('config')->urls->PageRenameOptions . "InitialDifferenceProtected.js?v={$version}");
        }

 

  • Like 1
Link to comment
Share on other sites

24 minutes ago, adrian said:

What about if the module were to just disable the Initial Differences Protected check if the name doesn't match the title but the title contains (copy\s?\d?\) which would match (copy), (copy 2), etc. I think this approach will work better. Thoughts?

That would be good for the English default, but the (copy) part is translatable so it would be difficult to get it working for translations.

Link to comment
Share on other sites

To further complicate things, it seems that PageRenameOptions might be interfering with the normal clone titles. When the module isn't installed the (copy 2), (copy 3), etc is what I would expect...

image.png.cdba10f3163abbcb564832b5aa665841.png

...but when PageRenameOptions is installed I get duplicates like this (edit: to be clear, this is the result after 6 clones)...

image.png.fc2ef5b55c0cd6b7a70a4a6ab284658a.png

If the duplicate thing didn't happen then maybe the page names would always match the title without anything else needed? (assuming the hook you suggested)

Edit: here are my PageRenameOptions settings:

{"PageRenameOptions":{"version":"2.0.1","settings":{"enableForApi":1,"renameMatchTitle":1,"renameWithoutTitleChange":"","renameUnpublishedOnly":"","includeExclude":"exclude","initialDifference":1,"preventManualChanges":1,"liveChanges":null,"uninstall":"","submit_save_module":"Submit","exemptRolesInitialDifferencesProtected":[],"exemptRolesPreventManualChanges":["superuser"],"exemptRolesLiveChangesProtected":["superuser"],"specifiedPages":[],"specifiedTemplates":[],"initialDifferencesProtected":1,"liveChangesProtected":null}}}

 

Link to comment
Share on other sites

Good point about the translation of "copy". 

As for the duplicate issue - I just tested here and uninstalling the module didn't completely prevent duplicate names. I think issue you saw might be related to pages created when you were experimenting with the Pages:cloned hook code I posted above. But please let me know if you see the problem once all those versions are removed and you're start with a fresh set of clones.

Link to comment
Share on other sites

@adrian, thanks for looking into this request but after some more investigation I don't think there will be a good way for PageRenameOptions to deal with the ProcessPageClone issue. It's something that would need to be addressed within ProcessPageClone. It works out the copy number - (copy 2), (copy 3), etc - by looking at the page name to check for an existing number suffix. So it needs to name the first clone with a "1" suffix and yet it wants to title that first clone without the "1". If we change PageRenameOptions to set the clone name from the title then there's no number suffix on the first clone and so it messes up the calculation for subsequent clones.

I don't think there will be changes to ProcessPageClone any time soon, and in fact the whole problem would largely be avoided if my request from 2018 was actioned.

So instead I'm going to solve this with a couple of extra hooks:

// Make ProcessPageClone always use "Copy Page" form
// https://github.com/processwire/processwire-requests/issues/186
$wire->addHookAfter('ProcessPageListActions::getExtraActions', function(HookEvent $event) {
	$page = $event->arguments(0);
	$actions = $event->return;
	if(empty($actions['copy'])) return;
	$config = $event->wire()->config;
	$actions['copy']['url'] = "{$config->urls->admin}page/clone/?id={$page->id}";
	$event->return = $actions;
}, ['priority' => 200]);

// Set "Copy Page" name field to match title field
$wire->addHookAfter('ProcessPageClone::buildForm', function(HookEvent $event) {
	/** @var InputfieldForm $form */
	$form = $event->return;
	$title_field = $form->getChildByName('clone_page_title');
	$name_field = $form->getChildByName('clone_page_name');
	if($title_field && $name_field) {
		$name_field->value = $event->wire()->sanitizer->pageName($title_field->value, Sanitizer::translate);
	}
});

With these hooks PageRenameOptions will do its normal thing and sync the name field as I type a new title in the ProcessPageClone "Copy Page" form.

  • 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
  • Recently Browsing   0 members

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