Jump to content
Leftfield

Avoid default checking page->name

Recommended Posts

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?
 

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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):

photo-2.png.dfecbd79efbe1d87224328e3c3bc4785.png
photo-3.png.84949f415d959931428798ec4484df85.png

# Backend administration:

photo-5.thumb.png.4b9afe7d632a9319524004ec06442ac2.png

# Page tree:
photo-1.png.0f71b6dc8509eb7cc68279fc5bfe4734.png

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..).

  • Like 4

Share this post


Link to post
Share on other sites

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/

kats.thumb.PNG.6692c8630d5b3356f0de1f79ff7fe425.PNG

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.

details-field.thumb.PNG.9e55e5c1451a37bce5c48b8a42521a70.PNG

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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...