Jump to content

Custom Paths


Robin S
 Share

Recommended Posts

A new module that hasn't had a lot of testing yet. Please do your own testing before deploying on any production website.

Custom Paths

Allows any page to have a custom path/URL.

Note: Custom Paths is incompatible with the core LanguageSupportPageNames module. I have no experience working with LanguageSupportPageNames or multi-language sites in general so I'm not in a position to work out if a fix is possible. If anyone with multi-language experience can contribute a fix it would be much appreciated!

Screenshot

custom-paths

Usage

The module creates a field named custom_path on install. Add the custom_path field to the template of any page you want to set a custom path for. Whatever path is entered into this field determines the path and URL of the page ($page->path and $page->url). Page numbers and URL segments are supported if these are enabled for the template, and previous custom paths are managed by PagePathHistory if that module is installed.

The custom_path field appears on the Settings tab in Page Edit by default but there is an option in the module configuration to disable this if you want to position the field among the other template fields.

If the custom_path field is populated for a page it should be a path that is relative to the site root and that starts with a forward slash. The module prevents the same custom path being set for more than one page.

The custom_path value takes precedence over any ProcessWire path. You can even override the Home page by setting a custom path of "/" for a page.

It is highly recommended to set access controls on the custom_path field so that only privileged roles can edit it: superuser-only is recommended.

It is up to the user to set and maintain suitable custom paths for any pages where the module is in use. Make sure your custom paths are compatible with ProcessWire's $config and .htaccess settings, and if you are basing the custom path on the names of parent pages you will probably want to have a strategy for updating custom paths if parent pages are renamed or moved.

Example hooks to Pages::saveReady

You might want to use a Pages::saveReady hook to automatically set the custom path for some pages. Below are a couple of examples.

1. In this example the start of the custom path is fixed but the end of the path will update dynamically according to the name of the page:

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template == 'my_template') {
        $page->custom_path = "/some-custom/path-segments/$page->name/";
    }
});

2. The Custom Paths module adds a new Page::realPath method/property that can be used to get the "real" ProcessWire path to a page that might have a custom path set. In this example the custom path for news items is derived from the real ProcessWire path but a parent named "news-items" is removed:

$pages->addHookAfter('saveReady', function(HookEvent $event) {
    $page = $event->arguments(0);
    if($page->template == 'news_item') {
        $page->custom_path = str_replace('/news-items/', '/', $page->realPath);
    }
});

Caveats

The custom paths will be used automatically for links created in CKEditor fields, but if you have the "link abstraction" option enabled for CKEditor fields (Details > Markup/HTML (Content Type) > HTML Options) then you will see notices from MarkupQA warning you that it is unable to resolve the links.

Installation

Install the Custom Paths module.

Uninstallation

The custom_path field is not automatically deleted when the module is uninstalled. You can delete it manually if the field is no longer needed.

 

https://github.com/Toutouwai/CustomPaths
https://modules.processwire.com/modules/custom-paths/

  • Like 18
  • Thanks 2
Link to comment
Share on other sites

  • 4 months later...

@Robin S thanks for the Module.

I installed the module and added it to my template.
But it looks like PagePathHistory is overwriting the new custom path.

my old path:
/test/folder/page/

new custom path
/test/page/

When i enter domain.com/test/page/, the site redirects me to the old path.
Did i forgot something?

 

 

 

 

Link to comment
Share on other sites

4 hours ago, ngrmm said:

But it looks like PagePathHistory is overwriting the new custom path.

I can't reproduce that. If I set the custom path to something that it is in PagePathHistory for the page then I can successfully view the page at the custom path without being redirected by PagePathHistory.

2020-09-17_085517.png.3924e2ad65e1a2b7414e763badfaf794.png

2020-09-17_085550.png.b9c911713f22e80258243f6eeca222d8.png

2020-09-17_085610.png.89d040b0edec9a847d6ed20d1530fddc.png

If you can give me some steps to follow that will consistently reproduce the problem on a clean installation let me know and I'll try and investigate further.

 

 

 

  • Like 1
Link to comment
Share on other sites

@Robin S was very kind and had a deeper look at my problem and found the problem:

Quote

I took a look and it seems that the Custom Paths module is incompatible with the core LanguageSupportPageNames module. LanguageSupportPageNames also hooks Page::path and performs redirects before pages are viewed (not sure how it works exactly), and so it might be that there's no way for these two modules to function together.

 

  • Like 1
Link to comment
Share on other sites

I've added a note about the incompatibility with LanguageSupportPageNames to the module readme. I'm not sure how LanguageSupportPageNames works exactly or what part it plays in multi-language functionality but I can see that, like Custom Paths, it hooks Page::path and performs redirects before pages are viewed. This makes me think it may be impossible for these two modules to work together, but if anyone with multi-language experience can see a solution I'd be glad to apply any fix.

  • Like 1
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

×
×
  • Create New...