Jump to content

Minor API update for when saving a new page


ryan
 Share

Recommended Posts

This is relatively minor, but I realized a lot of us were repeatedly using the same code to populate a required 'name' field when creating new pages via the API. As a result, I figured I'd build it in. Here's the commit note:

Update $pages->save() so that it auto-generates a $page->name field when none is provided in the new page. It generates it from the 'title' field. This makes for more convenient API usage, especially when importing lots of pages. It also accounts for duplicate name fields and adds an incrementing number to the end when it finds duplicates.

Previously if you tried to save a page without a 'name' field it would throw an exception. Now it won't, as long as the page has a title. This should not affect any existing code. But it does mean that future page imports may be a little simpler since you don't have to consider the 'name' field.

Link to comment
Share on other sites

Ryan, i tried with latest commit but i get a page name "0". No exception but name of the page is not the title...

simple code:

<?php
$s = new Page();
$s->template 	= $templates->get("submit");
$s->parent	= $pages->get($userid);
$s->title     = "Submissions";
$s->save();

Maybe because my parent page is an user?  ???

Link to comment
Share on other sites

Sevarf2, I can't seem to duplicate here. I started from a new install and pasted this into a template:

<?php
$p = new Page();
$p->template = $templates->get('basic-page');
$p->parent = $pages->get($user->id);
$p->title = 'Submissions';
$p->save();

Then ended up with this page:

/processwire/access/users/admin/submissions/

Can you think of any other factors or other modules that might be running? I'm also curious about the page name "0". I just committed an update that may fix this in your case–can you grab the latest commit and let me know if that fixes it?

Thanks,

Ryan

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...