Leftfield Posted September 18, 2019 Share Posted September 18, 2019 Hi @All ? I made custom URLs I have unique hash at the end of the every URL of template=product: /nike-air-max-man-running-trainers/pr/832a2805/ Now, I don't need PW to check is there a duplicate page name because it will add to URLs numbers like this (-1, -2, -3...): /nike-air-max-man-running-trainers/pr/832a2805/ /nike-air-max-man-running-trainers-1/pr/9876j536/ /nike-air-max-man-running-trainers-2/pr/54b5221/ /nike-air-max-man-running-trainers-3/pr/0o542761/ etc. How to stop PW from doing this? Link to comment Share on other sites More sharing options...
Robin S Posted September 19, 2019 Share Posted September 19, 2019 In PW you cannot have multiple sibling pages with exactly the same name. That's why the pages are automatically renamed - otherwise you would see an error message. 1 Link to comment Share on other sites More sharing options...
Leftfield Posted September 19, 2019 Author Share Posted September 19, 2019 One of the easiest solutions in my head is: to exclude "/" from regex when creating page->name. Or to query page->title after save, remove number and copy database page_paths path to pages name after save. Hash is unique and that's what makes the page unique. Link to comment Share on other sites More sharing options...
OLSA Posted September 19, 2019 Share Posted September 19, 2019 Hello for all, and for my friend @Leftfield Sorry, but I need to say that I agree with @wbmnfktr in your previous post ("... why do you want/need such a weird URL?")?Here is one solution (as option) with default Processwire (no hooks, custom modules or core hack): 1) template: product, fields: title, details (page table with auto name date format) 2) child template: details, fields: image, color, size, price # With that you will get paths (URL's): # Backend administration: # Page tree: There is always option to develope custom module with different UI (and Ajax processing) where user in one "container" write title (parent page), and in another product details (child page). For users all that can looks just like a single UI form that they don't know that are 2 pages. Right now, I see this as one option (or something in this direction) how to get that what you want, but also in all that respect PW core. But need to think is this all worth it (complexity in backend, and later in frontend..). 4 Link to comment Share on other sites More sharing options...
Leftfield Posted September 19, 2019 Author Share Posted September 19, 2019 Good one! Resolves everything! Link to comment Share on other sites More sharing options...
Leftfield Posted September 21, 2019 Author Share Posted September 21, 2019 Here is final solution for the link: /samsung-s10-mobile-phone/pr/f980fd84/ I got tree like this:Products/product articles/details NOTE: template articles has name/URL: "pr" NOTE 2: Product is the main name of the article/articles but all other fields are going in the template details. It was made in case of duplicate name so the hash would be The only difference but the product page would keep children with hash name. See the URLs and picture bellow /samsung-s10-mobile-phone/pr/f980fd84/ /samsung-s10-mobile-phone/pr/d8h3jk1n/ NOTE 3: I am using hidden hash_field as text field on the template details for recording hash URL segmentation enabled on all templates. In product template there is: Profields: Page Table (with the same name as template "Details") with action on product page. Module for making hash: <?php namespace ProcessWire; class AddHashField extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Add Hash Field', 'summary' => 'Populate hash field of product template', 'href' => '', 'version' => 001, 'permanent' => false, 'autoload' => true, 'singular' => true, ); } public function init() { $this->pages->addHookBefore('saveReady', $this, 'AddHashField'); } public function AddHashField($event) { $page = $event->arguments[0]; if($page->template != 'details') return; if(!$page->title){ $hash = hash('CRC32', microtime()); $page->set('title', $hash); $page->set('name', $hash); $page->set('hash_field', $hash); $this->message("Hash is set " . $page->hash_field); } } } In my (probably yours home.php) _main.php file, on the top: <?php namespace ProcessWire; if ($input->urlSegment1 && $input->urlSegment2 == 'pr' && $input->urlSegment3) : $pagename = $input->urlSegment1; $hash = $input->urlSegment3; $match = $page->find("template=details, name={$hash}"); include('views/' . $match->template . 'details.php'); return $this->halt(); endif; On details template: <?php namespace ProcessWire; $parent_name = $input->urlSegment1; $name = $input->urlSegment3; $parent_page = $page->findOne("template=product, name={$parent_name}"); $page = $page->findOne("template=details, name={$name}"); include("./head.inc"); include('./nav.php'); ?> <h1><?= $parent_page->title ?></h1> Function detailsUrl in function.php: function detailsUrl($item){ $out = ''; foreach($item->details as $c){ $out .= '<a href="' .$item->name .'/'. $item->parent->name .'/'. $c->name . '">' . $c->headline . '</a>'; } $out.= '<br />'; return $out; } Listing on the home template: <?php $pages = $pages->find("template=product, sort=date, limit=10"); foreach($pages as $item) : echo detailsUrl($item); endforeach; ?> Now, this is not finished code in home.php, it doesn't list on all products... But I leave your imagination how to handle further. IF anyone needs this rare solution I am here to help. Thanks to motherf.... @OLSA (we are friends) who doesn't let me give him cup. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now