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 Martin Muzatko
      Hello!
      I'm trying to use the data I create in Processwire as much as possible.
      So for a form, I try to use the fields description, name and also its built-in validation rules I defined in ProcessWire on the front-end. (minlength, ranges, patterns, etc)
      I already looked into this tutorial, but it is using external resources to validate the form.
      Since ProcessWire does all the heavy lifting, when processing data, I don't have to sanitize anything - ___processInput should do the job just fine.
      However, it is not actually working correctly. 
      $fields = $templates->get('user')->fields; $submission = $input->post; foreach ($submission as $key => $value) { $field = $fields->{$key}; if ($field instanceof Field) { $field = $field->getInputfield($user); $field->___processInput(new WireInputData([$key => $value])); var_dump($field->getErrors(true)); // retrieve validation error } } This works for some constraints, but the values are not correctly validated.
      Example: 

      All the fields are required and zip is an integer field.
      Yet, I get no validation error for zip, although it was entered as a string, and not an integer. Funny enough: if I provide a number outside the range, I get "Specified value 2 removed because it is out of bounds (min=1000, max=99999)".
      firstname will not return any error for being a required field.
      From what I have looked through the source code, there is no check for "required". Some fields only validate on setAttribute. Am I missing anything or am I doomed to build my own validation process?
      Thank you in advance!
      Best,
      Martin
    • 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?