Sign in to follow this  
bmacnaughton

Create page name with multiple consecutive dashes

Recommended Posts

bmacnaughton    30

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
bmacnaughton    30
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
bmacnaughton    30
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
Zeka    282

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

  • Like 3

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  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By sirhc
      Hi all,
      Im looking for a selector that gets the pages added a specific time frame (eg. from yesterday 9:00pm to today 9:00pm).
      Is there a existing selector im missing or does someone now a good solution for this?
      Thanks in advance .
    • By celfred
      Hello,
      Just a simple (I think) question which is in the title of my post.
      Roughly speaking : here's my code (made-up because my real function is so long... I don't want to post it all here )

      function updateScore($player, $task, $real = true) { [...] $player->score = $player->score+$task->score; if ($task->name == 'new-equipment') {   $new-eq = $pages->get("name=sword");   $player->equipment->add($new-eq); } if ($real == true) {   $player->save(); } }
      Everything works fine when I call updateScore($player, $task, true), but if I call updateScore($player, $task, false), scores are untouched, but the equipment gets added !
      It used to work fine on PW2.7 but my update to PW 3.0.62 seems to have broken this... Is there a simple explanation ? I keep reading my code over and over and this is driving me crazy...
      Thanks !
    • By zaib
      I've a page "blog" which have child pages (blog posts). Right now I've 11 pages (blog posts) and I'm fetching all the posts in my "blog" pages which displaying fine, Issue is only 10 results are showing this is how I'm fetching
      $entries = $pages->find("template=blog-entry"); foreach($entries as $entry){ <a href='{$entry->url}'>$entry->title</a> }  
    • By pppws
      hey there,
      i'm quite new to processwire but i'm having a great experience using it!
      right now i hit a point where i can't help myself out with google/searchfunction.
       
      to sketch the basic functions of my page:
      visitors can enter their e-mail in a form. when doing this pw creates a new page, using the e-mail's md5 hash as a name. the url of the page is sent to the user. the user now can edit the newly created page and can fill out fields like: name and year of birth (thanks to the docs those two already work like a charm!) but there are other fields like "residencies", which are repeaterfields containing three fields: city (text), country (text), current (checkbox). at my current version i can populate a new repeater field easily by using this code:
      $location = $page->locations->getNew(); $location->location = Munich; $location->country = Germany; $location->current = 1; $location->save(); $page->locations->add($location); now i want to populate the new reapter fields from input fields (using the simple form api). which leads me to my questions:
       
      1. can i somehow group/merge input fields to one "repeater input fields" right now i have:
      // create a text input -> locations $field = $modules->get("InputfieldText"); $field->label = __('City'); $field->attr('id+name','location'); $field->required = 1; $form->append($field); // append the field to the form // create a text input -> country $field = $modules->get("InputfieldText"); $field->label = __('Country'); $field->attr('id+name','country'); $field->required = 1; $form->append($field); // append the field to the form // create a checkbox -> locations $field = $this->modules->get('InputfieldCheckbox'); $field->attr('name', 'location_current'); $field->attr('autocheck', 1); $field->attr('uncheckedValue', 0); $field->attr('checkedValue', 1); $field->attr('value', $this->fname); $form->append($field); // append the field to the form 2. my desired layout for the form looks like this:

      by hitting the + button a new line shows. i'm wondering what's the best practice here. can is somehow use the above mentioned code itself as a repeater, or do i have to create several (uniqe) input fields in advance and hide them afterwards (javascript)?
       
      any help is appreciated!
      thanks!
    • By alexcapes
      Hi,
      I've been invited by Apple to use their 'News' service for a publication in the UK. https://www.apple.com/uk/news/
      A number of CMSs already have plugins to publish content via the API, however Processwire is not one (yet!)
      https://newsresources.apple.com/en/faq/66346243
      It's slightly beyond my technical abilities but would be very interested in anyone that is willing to look at developing a Processwire Module.
      (for reference here is the Apple News API documentation)
      Update: Chapter Three have created a PHP Apple News library here https://github.com/chapter-three/AppleNewsAPI