Jump to content

Absolute beginner: how to add pages using the API


pop
 Share

Recommended Posts

Hi everybody. I'm new to ProcessWire and I think it might be just what I need for some of my projects.

As a starter, I would like to write a small web application, and part of the application should be able to add new pages and change existing ones using the API (as opposed to the Admin interface). I think I have read all of the tutorials and a sizeable part of this forum, still I can't find out how such an application would look like in PW. Also, there was mention of a Wiki which I couldn't locate, either.

So, this is how I think such an application should work:

  • Let's say the web application manages a number of products.
  • The product directory lists all available products, i.e. it shows a list of the titles of each product page.
  • Klicking on a link named "new product" will open a page with an empty form. The user fills in all the fields in the form and then klicks on a button named "submit".
  • The server then validates the contents of all the fields, creates a new page (taking the title and the URL from the form), moves the contents of the fields in the form into the fields of the page and then shows the newly created product page.

Questions:

  • Is this how new pages are added when using the API (and not the admin interface)?
  • What do I call the page with the empty form and where in the hierarchy does it live? Is it even a page within the hierarchy, when it does not have any underlying data fields of its own?
  • Where does the completed form send (post) its content to, given that the page obviously does not exist yet?

Thanks in advance to anyone who can bring light into my darkness.

Link to comment
Share on other sites

4 hours ago, pop said:

I'm new to ProcessWire and I think it might be just what I need for some of my projects.

Sounds like a plan!

I'd start asking if you have enough understanding on how "routing" in processwire works as well as template/fields concept and how they relate to pages. With that said, I'll try to quickly illustrate an example:

4 hours ago, pop said:
  • Is this how new pages are added when using the API (and not the admin interface)?
  • What do I call the page with the empty form and where in the hierarchy does it live? Is it even a page within the hierarchy, when it does not have any underlying data fields of its own?
  • Where does the completed form send (post) its content to, given that the page obviously does not exist yet?

This is definitely a way you can add content using the API. Let's assume you have a template named "new-product", and a page "New Product" with said template under Home page. You end up with a page tree like this:

Home
-- New product
-- Products
--- Product 1
--- Product 2

This page doesn't need to have fields, it's just a place (a route) to handle code with /site/templates/new-product.php template file, which is automagically assigned to every page with the template "new-product". Here is some sample code that would live on that template.

if($input->post->submit){
	//Do stuff like sanitizing and validation, very important
    // Some basic page creation saving would look like this
    $newPage = new Page();
    $page->template = "product";
	$page->parent = $pages->get('template=products');
    $page->title = $sanitizedTitle // This comes from the imaginary code above
    $page->save();
} else{
    //Do the form rendering however you want.
}

This way user arrive to /new-product/, submits a form, and you handle the page creation right there. 

As you can see, products get a parent page, which is the page called Products, where I'm also assuming it has a template named "products", it is another tempalte without any required fields in the current context, it just handles the organization of the products. You would end with each product under:

/products/product-1
/products/product-2

Last but not least, FormBuilder pro module handles the form-to-page process basically out of the box.

  • Like 3
Link to comment
Share on other sites

Thank you very much, this is very useful. I'll have a look at the FormBuilder later.

Perhaps someone could do a tutorial on adding and updating pages using forms and the API? I'm quite sure I'm not the only beginner who finds this non-trivial.

Link to comment
Share on other sites

2 hours ago, pop said:

a tutorial on adding and updating pages using forms and the API?

Hi @pop

You might missed this one topic :

 

 

Then use google/this tool ? with the right keywords, you will find a ton of topics - thanks @mr-fan

 

And do not hesitate to ask, as you can see, you will get nice answers ?

  • Like 1
Link to comment
Share on other sites

16 hours ago, flydev ?? said:

You might missed this one topic :

Hi flydev

Thanks a lot for this one. I did indeed miss it as I was looking for an introduction into how inserts and updates are done. As it's titled "Creating a simple form", it did not occur to me that it might apply as well. Creating a form would have been next on my to do list, so this is doubly welcome.

Thanks also for advice on searching. However, I did a very thorough search on this site and elsewhere and I haven't been able to locate anything on the design pattern suggested for what I was looking for. The number of threads and tutorials I have been going through was heartbreaking, and (as I said before) most of the material appeared to be dated. References to the wiki abound. It appears that the wiki was abandoned some time ago.

As I am still evaluating whether ProcessWire will fit my needs, I won't likely install anything in my browser just now.

Link to comment
Share on other sites

Yes, I don't use the wiki but do not be afraid by those old dated thread - check- they will mostly work, I mean, I installed yesterday a module made in 2013 still working without any modifications on the last dev version. Isn't crazy ? ?

 

https://www.pwtuts.com/

https://processwire.dev/

https://cse.google.com/cse?cx=014789015761400632609:fxrf0rj4wr4

and as always, linking this old thread with old videos which still work with the latest processwire version :

 

 

  • Like 2
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...