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 ttttim
      I'm building an upload form for user to add images to a page. I would like for the user to change the image order after they add the images. Basically how the image field in the backend works. So users can drag and drop the image order. 
      The problem is that I don't know how to rearrange the data of my image field based on the order a user submits?
       
    • By cosmicsafari
      Hi all,
      I am working on a module which creates templates, fields & pages upon install.
      I have ran into an issue where I have a numerous fields which I would like to override their labels depending on the template they are used in.
      Is it possible to do this dynamically via API at the point where I am creating the field group and template using the API.
      The following is the code which I am using at the moment.
      foreach($templatesArray as $name => $cTemplate) { if(array_key_exists('fieldGroup',$cTemplate)) { $fg = new Fieldgroup(); $fg->name = $name . '-fg'; foreach ($cTemplate['fieldGroup'] as $field) { //possibly at this point? $fg->add($this->fields->get($field)); } $fg->save(); } $t = new Template(); $t->name = $name; $t->fieldgroup = $fg; $t->noChildren = 1; $t->setIcon($cTemplate['icon']); $t->save(); } Is this even possible?
    • By cosmicsafari
      Hi All,
      I am working on a module at the moment which in theory will create a few simple fields, templates and pages which will all be linked together.
      All is going well so far, I have it generating said fields, templates and pages, however I would like to also set the pages sort order and icon programmatically at this stage also.
      I have yet to see any documentation or any other threads where this has been brought up, is it even possible programmatically without interacting with the database directly?
      This is the snippet which is generating my pages on the fly at the moment.
      foreach($arrayOfPages as $name => $page) { $p = new Page(); $p->template = 'page'; $p->name = $page['URL']; $p->title = $name; $p->parent = wire('pages')->get('name='.$page['parent']); //is it possible to set the icon and sort order at this point, before saving? $p->save(); }  
    • By bmacnaughton
      I have a hook that creates a page for a subset of the pages on our site. It uses the saved page's name as part of the created page's name.
      The problem I am having is that my hook, attached to Pages::saved(), is being called even when the page save failed because of missing fields. Is there a way I can tell that the page save failed due to missing fields?
       
      Never mind - it does succeed; it just issues warnings about required fields.
       
    • By Harmen
      Hi folks,
       
      On the website I am working on I do have a form which needs to be filled in (duh) and when the user clicks on download, the data is send with ajax and processed in the same file by using the ProcessWire API with $config->ajax;
      The data which is provided by the user will be checked with data in my database. If the data is correct I want to push a file download. But this is not working at the moment. It seems like the content of the (.exe) file is pasted in the console instead of pushing the file. My code looks like this:
      if ($config->ajax){ $serial = $input->post('serial'); $DB = DB(); $query = $DB->prepare("/*My query here*/"); $query->bindParam("comparision", $serial, PDO::PARAM_STR); $query->execute(); $reply = ''; $SN_rows = $query->rowCount(); if (!$SN_rows > 0) { $reply = 'error'; } else { $reply = 'success'; $installer = $page->attachments->eq(1); $filename = "Filename"; $filepath = $installer->filename; $options = array( 'forceDownload' => true, 'exit' => true, 'downloadFilename' => $filename ); wireSendFile($filepath, $options); } echo $reply; exit(); } What am I doing wrong here or is it a bug? Is there another way to do this with the processwire API?
       
      Thanks in advance,
       
      ~Harmen