Matt Cohen Posted May 27, 2016 Posted May 27, 2016 Hi there, I have an issue with my code where when it goes to save the page - it saves it using an ugly unix time stamp. Example: /lost-property/2016-05-25-13-40-16/ What I am wanting to do is instead of saving as timestamp, I'd like to use the id of the page. /lost-property/42854 My code is <? if ($input->post->lp_date) { // Save in the ProcessWire page tree; map submission to the template fields $np = new Page(); // create new page object $np->template = $templates->get("lost-property"); $np->parent = $pages->get("/lost-property/"); // Send all form submissions through ProcessWire sanitization $lp_date = $sanitizer->text($input->post->lp_date); $lp_type = $sanitizer->text ($input->post->lp_type); $lp_city = $sanitizer->text($input->post->lp_city); $lp_row = $sanitizer->text($input->post->lp_row); $lp_item = $sanitizer->text($input->post->lp_item); $lp_description = $sanitizer->text($input->post->lp_description); $lp_contactname = $sanitizer->text($input->post->lp_contactname); $lp_contact = $sanitizer->text($input->post->lp_contact); $lp_ro = $sanitizer->text($input->post->lp_ro); $lp_status = $sanitizer->text($input->post->lp_status); // Match up the sanitized inputs we just got with the template fields $np->of(false); $np->title = $title; $np->lp_type = $lp_type; $np->lp_date = $lp_date; $np->lp_city = $lp_city; $np->lp_row = $lp_row; $np->lp_item = $lp_item; $np->lp_description = $lp_description; $np->lp_contactname = $lp_contactname; $np->lp_contact = $lp_contact; $np->lp_ro = $lp_ro; $np->lp_status = 'open'; // Set the status to 1 (confrimed ) // Save/create the page $np->save(); ?>
Robin S Posted May 27, 2016 Posted May 27, 2016 $np->title = $title; Where does the $title variable come from in your code? You don't show where it is set. To set the URL you want to set the name of your page. $np->name = 'something'; If you don't set a name (as in your code) the name is generated automatically from the title. If you are setting a name from a variable you can pass it through $sanitizer->name($my_variable) to make sure it is a valid name. In your case you want to set the name to the same as the page ID. But when you are first creating your page the ID doesn't exist until you save it. So you can do something like this: $np = new Page(); // create new page object $np->template = $templates->get("lost-property"); $np->parent = $pages->get("/lost-property/"); $np->title = 'my title'; $np->save(); // save the page so you can get its ID $np->name = $np->id; // set your other page fields $np->save(); 6
szabesz Posted May 27, 2016 Posted May 27, 2016 related forum topic: https://processwire.com/talk/topic/352-creating-pages-via-api/
renobird Posted May 27, 2016 Posted May 27, 2016 For reference, you can also make sure this happens when a page is saved in the admin as well. <?php class AdminHelperHooks extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Admin Helper Hooks', 'version' => 1, 'singular' => true, 'autoload' => true ); } public function init() { $this->addHookAfter('Pages::saveReady', $this, 'hookChangeNameToID'); } public function hookChangeNameToID(HookEvent $event){ $page = $event->arguments(0); // get the page being saved if ($page->template == 'lost-property'){ // check the template $page->name = $page->id; } } } 5
adrian Posted June 9, 2016 Posted June 9, 2016 Depending on your needs, you may prefer to use this module, rather than actually renaming the page: http://modules.processwire.com/modules/process-redirect-ids/ I am not saying it's a better approach - just different
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now