Sign in to follow this  
Followers 0
bmacnaughton

Create page name with multiple consecutive dashes

6 posts in this topic

I'm trying to create a pagename (for internal use, not for presentation to the users) with multiple dashes in the middle. Here's an excerpt of my code:

$p = new Page();

$p->template = 'template-name';
$p->name = "$prod--$variation";

ProcessWire seems to apply $sanitizer->pageName($name, true) which converts multiple dashes to a single dash so the page name doesn't have "--" in it.

How can I force it to have multiple dashes?

 

 

Edited by bmacnaughton
Show code to clarify

Share this post


Link to post
Share on other sites

Sorry if this is overly simplistic, but if that is truly all you seek to do, could you just

$url = str_replace("-", "--", $sanitizer->pageName($name, true));

 

1 person likes this

Share this post


Link to post
Share on other sites
5 minutes ago, Brian Scramlin said:

Sorry if this is overly simplistic, but if that is truly all you seek to do, could you just


$url = str_replace("-", "--", $sanitizer->pageName($name, true));

 

That doesn't actually change the page name though - I already have what I want in $name, right?

Share this post


Link to post
Share on other sites
Just now, Zeka said:

@bmacnaughton

Not sure, but maybe you are looking for 


allowDoubledReplacement (bool): Whether to allow two of the same replacement chars [-_] next to each other (default=false).

See options argument

https://processwire.com/api/ref/sanitizer/name/

That's getting there - if I were in control of $sanitizer then I'd be set. But I'm creating a page via API so my code looks like (and sorry, I should have included this):

$p = new Page();

$p->template = 'template-name';
$p->name = "$prod--$variation";

So I don't have control over calling $sanitizer - PW is doing that internally. 

Share this post


Link to post
Share on other sites

Okey, I see.

From sanitizer docs:

 * Modules may also add methods to the Sanitizer as needed i.e. $this->sanitizer->addHook('myMethod', $myClass, 'myMethod'); 

So you can add custom sanitizer method and then hook __construct method of InputfieldName and set your custom method for some pages

https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldName.module#L19

3 people like this

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By bmacnaughton
      Do any experts have experience with the level of stability and/or support for turning on $config->advanced? I ask because of the warning at the bottom of the System tab "Please note that all of these system settings are intended for ProcessWire system development (not site development). Use them at your own risk."
      It provides two facilities that are invaluable to me (maybe because I don't know how to do them any other way).
      1) Disable Settings Tab on a per template basis
      2) Page Class Name - this makes it tweak a page while relying on the underlying Page class for the majority of functions.
      Are others using this setting in production? Have you used it over an extended time and seen that it is not changed often or at all?
    • By bmacnaughton
      I have a Page Table field that, for new entries, I want to construct the name for.
      When I click the Add New button the page being added has already been given a random name like: 20170422-003744.
      I wish to
      1) change that name so it's obvious it is a temporary name
      and 
      2) when either Publish or Save + Keep Unpublished is clicked, set the name of the page based on the content entered.
      What hooks can I use to accomplish 1 and 2?
       
      thanks.
       
    • By timothy.m.wilson
      Good afternoon,
      I am attempting to loop through approximately 600 XML nodes. Each of those nodes has 2 child nodes (example below). One is an id for an existing page and the other contains values for a page reference field. I have put together a loop (below as well) to attempt to save each page with the label values. This all works....once. After getting the first XML node "label" saved to the correct page, nothing else is saved. I'm sure there is probably something that I have overlooked. But, I am also very new to this CMS. I'm hoping there is a veteran user who may be able to point me in the right direction. 
       
      <row>
          <id>1041</id>
          <labels>lifestyle|savings</labels>
        </row>
       
      foreach ($xml->row as $items) {
              
              //get page id to update and set output formatting to false
              $blog_page = $pages->get('id='.$items->id);
              $blog_page->of(false);
              //create array from pipe separated label values
              $labelsArray = explode('|', $items->labels);
              //loop through array to get page ids by page name
              $labelIDS = array();
              foreach ($labelsArray as $labelValue) {
                  $labelPage = $pages->get('name='.$labelValue);
                  array_push($labelIDS, $labelPage->id);
                  
              }
              //add labels to label field in post
              $blog_page->labels = $labelIDS;
              
              //save page
              $blog_page->save();
          
      }
    • By bmacnaughton
      We have a three language web site - 'de', 'en', 'fr' with 'de' being the default.
      There are pages for which we want to use the name (it's unique within its parent-scope) as in the attachment.
      But when I fetch the name it does not return the name for the language - it always returns the name for the default language (guertel, in this case).
      I've tried $page->getLanguageValue($lang, 'name'), but name is not a "real" field, so it returns null.
      I can hack around it by extracting the last segment of the URL associated with that page (which is language sensitive) but it seems like I must be missing something.
      Is there some way to fetch $page->name for the $user->language setting?
       
       

    • By cosmicsafari
      Hi all,
      I'm just trying to update a custom field value for a page but I noticed in the DB that the old value is still there?
      Rather than updating, it appears to create a new record for the updated value and increments the sort value.
      Why is this? Is it not possible to just overwrite the value within the DB using the API?
      For example:
      $pageObject->of(false); $pageObject->customField = "Updated Value"; $pageObject->save(); $pageObject->of(true);  
      I would have expected the value just to be overwritten and persisted to the field table in the database?
      Obviously I am missing something or just not grasping why it works like this?