Jump to content

Creating Pages via API


Soma

Recommended Posts

Just wanted to post it here for others that might look for an example. I'm currently working on importing a Site to PW2.1.

Following code I figured is needed to create pages using the Bootstraped API:
 

<?php

include(./index.php) // bootstrap PW

$p = new Page(); // create new page object
$p->template = 'page'; // set template
$p->parent = wire('pages')->get('/about/'); // set the parent
$p->name = 'mynewpage_url'; // give it a name used in the url for the page
$p->title = 'My New Page'; // set page title (not neccessary but recommended)

// added by Ryan: save page in preparation for adding files (#1)

$p->save();

// populate fields

$p->image = 'path/to/image.jpg'; // populate a single image field (#2)
$p->images->add('path/to/image1.jpg'); // add multiple to images field

$p->save();

// testing

echo 'id: '.$p->id.'<br/>';
echo 'path: '.$p->path;

Note: in PW 3 with multi-instance support adding new Objects 

https://processwire.com/blog/posts/processwire-2.6.21-upgrades-comments-more-on-pw-3.x/#more-updates-on-processwire-3.0

In 3.x, if your module creates new objects, like $page = new Page();, you'll want to wire those new objects to the current ProcessWire instance, which makes it a "wired" object. You do it like this: $page = $this->wire(new Page());

[Edit by Ryan #1] Added first $p->save();
[Edit by Ryan #2] Changed $p->image('...') to $p->image = '...';

  • Like 14
Link to comment
Share on other sites

Thanks Soma, good post!

A couple things to mention:

1. You need to save a page before adding files to it. So you'd want to have another $p->save() before the part where you add an image. This is because in order to place a file in the page, it needs to exist on disk first (so that it has a place to copy the image to). Before you save a page, it only exists in memory. Though if you found that actually worked, let me know–maybe I found a solution in a late night coding session and forgot about it. :)

2. To add a single image, you'd need to do $p->image = 'path/to/image.jpg'; rather than $p->image('path/to/image.jpg'). This is because there is no Page::image() function. I updated your post for this.

3. For others looking at this code sample, you may need to add a $page->setOutputFormatting(false) depending on the context and if PW gives you an error about needing to turn off output formatting.

  • Like 2
Link to comment
Share on other sites

Thanks Ryan for clarifying things.

I just ran another search and found a post has already been made on this topic... :)http://processwire.com/talk/index.php/topic,401.0.html

I think I forgot to add the additional save before adding pictures... I already knew as I figured that out from another post. Thanks for fixing the code.

Something I came across when importing the image itself using: $p->image = "path/to/image.jpg";

I first had tried like you mentioned here: http://processwire.com/talk/index.php/topic,341.0.html

But using external URL like http://domain.com/images/image.jpg didn't work. So I had to use local server path (document root).

Link to comment
Share on other sites

If specifying a remote URL doesn't work, this is most likely why (from php.net):

If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.

Some hosts have allow_url_fopen disabled for security reasons. You may be able to specifically enable it using whatever method your host provides to let you modify the PHP settings.

Link to comment
Share on other sites

  • 1 year later...

I tried the example code above - everything worked fine.

However, when adding images, the new filenames are all without .jpg suffix. They are all named "image.___" instead of "image.jpg".

The originals (on the same server, in another folder) have .JPG (uppercase) suffixes. Does that have anything to do with it?

Anything I can do to keep the exact same image-names when creating pages and using $p->images->add('foo/blah.jpg') ?

The browsers display the images OK, but I'd rather keep .jpg suffixes (upper- or lowercase) instead of .___

Link to comment
Share on other sites

To be more precise, in "page edit" view, I can view the thumbnails OK, but clicking on any of these, I just see gibberish like JFIF^^gExifII* (12iCanonCanon EOS 20Dg5'g5'Adobe Photoshop CS2 Windows2006:05:06 17:58:16"'02212 FN V     ^f0100nv 2006:05:05 19:37:162006:05:05 in the lightbox. The images themselves are fine, been online since 2006 on photo-galleries...

Link to comment
Share on other sites

I'm confused, where are the .___ images coming from? That's not an extension that ProcessWire uses or would recognize. Allowed extensions for images are jpeg, jpg, png, or gif. What I'm not clear about is whether you are trying to import images with that ___ extension, or if it appears they are somehow getting generated by PW?

Link to comment
Share on other sites

I was confused too.

The original images were in another folder on the same server. I entered the full path to the images. The original images are regular JPGs, with .jpg suffixes. The only thing that I could think of that may have triggered this strange PW behavior is: the image suffixes are uppercase: so, foo.JPG, not foo.jpg. (the uppercase suffix was generated from an old digital camera - but that didn't cause any problem whatsoever in the past)

I really could have used this handy API function to move / add literally thousands of images with one handy script (I'm re-building an entire photoblog site with PW).

There's nothing special about the images. Plain-jane, regular JPGs, exported from Photoshop with "save for web" menu. o_O

Link to comment
Share on other sites

To me it sounds like our filename sanitizer gone astray. Like if it was performing a replacement of non-alphanumeric characters before doing an strtolower. I will try to duplicate here and let you know what I find.  

Link to comment
Share on other sites

  • 3 weeks later...

Just a little note to let you all know I just love PW even more after using the few lines above to create my import module for some plain old html website. It is so easy to use the PW api... Thanks guys!


Perhaps you can extend it a bit more with some exception catching to make a more 'real world' example. But don't know if that is the scope of this post (or posts like this).

I could post my import example, it is the same as above but just a little more.

  • Like 2
Link to comment
Share on other sites

  • 7 months later...

Hello, didn't want to start a new thread for a simple question:

In the module processPageDelete - there is this line - 

$page = new Page(); 
.
.
$page->process = $this;

in ___install() . I can't figure out what this line does. I mean it is assigning the current object to the var $page->process. But looking through the Page class , I can't find this any $process. wondering what does it do and if it is required... anyone ?

Thanks

Edit: does it specify the object which contains the ___execute () for this page

Link to comment
Share on other sites

You're right. "process" is a field on the "admin" template, to select the module that defines the process for the admin page.

So it's simply saying "make the process of this page this module". And yes the execute() is used as the default method to return the content for the admin page, basicly what you see when you open an admin page, for example /processwire/myadminpage/.

Further urls segments can be used by adding other executeName() methods, where Name is the urls segment appended to the admin page url. Like /processwire/myadminpage/save/ would map to a executeSave() method in the process module. If it does exists, it will use it instead of execute() /processwire/myadminpage/mymethod/ would map to a executeMyMethod(). I think it's case insensitive.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...

Hi.

I need to create a page when the page was edited or created.

I wrote the hook as describet there https://github.com/processwire-recipes/Recipes/blob/master/extending-page-save-process.md

And placed in it the code like in the beginning of this topic. 

The problem is - if there is 




$p->save(); 

line exists - the images that I delete in the editing page are not deleted (((

Link to comment
Share on other sites

  • 4 months later...

Hi there,

When Creating Pages via API in Processwire..

Is there a way to create the specific page id of the page you created not auto increment.

Example: I have a existing page: page_id = 4254

I delete this page and created again the page.. it generate a new page id but i want to use the old page id 4254 in this new created  page?

Is this possible?..

Thanks in advance..

Link to comment
Share on other sites

Hi there,

When Creating Pages via API in Processwire..

Is there a way to create the specific page id of the page you created not auto increment.

Example: I have a existing page: page_id = 4254

I delete this page and created again the page.. it generate a new page id but i want to use the old page id 4254 in this new created  page?

Is this possible?..

Thanks in advance..

You can set the id like this:

$p = new Page();
$p->template = "basic-page";
$p->parent = 1;
$p->title = "Page Title";
$p->id = 4254;
$p->of(false);
$p->save();
Link to comment
Share on other sites

  • 3 weeks later...

Just wanted to post it here for others that might look for an example. I'm currently working on importing a Site to PW2.1.

Following code I figured is needed to create pages using the Bootstraped API:

<snip>

Anyone knows a good way to create a php-file that can be executed by php on the commandline (php.exe on windows), to do the same thing?

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
  • Recently Browsing   0 members

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