Add Page Programmatically

Permalink 1 user found helpful
I am trying to establish an AJAX process that adds a Page. I am attempting to load any Libraries and Models needed. Have checked this post for confirmation of coding:

The process I have, successfully adds a Collection and a Collection Version, but not a Page :(

Here is the set of Libraries and Models I am loading:


Here is the relevant code:

$pData = array('uID' => 12, 'pkgID' => 0, 'cName' => 'Test One Page', 'cHandle' => 'testone', 'cDatePublic' => '2011-03-01');
$oPage = Page::getByPath('/professionals');
$ct = CollectionType::getByHandle('professionals');
$newPage = $oPage->add($ct, $pData);

Here is the error:
[Thu Mar 17 16:50:25 2011] [error] PHP Fatal error:  Call to a member function getCollectionTypeID() on a non-object in /Library/WebServer/Documents/concrete5/concrete/models/page.php on line 1968

Any help appreciated.


View Replies: View Best Answer
jordanlev replied on at Permalink Reply
Your code looks right to me. Two possibilities come to mind:

1) Are you running this code from a file in the tools directory? And are you calling that file by using c5's tools helper, like this:
$th = Loader::helper('concrete/urls'); 
$th->getToolsURL('file_name_without_php_extension', 'package_name')); //<-- or without the package name if the file is just in your site's /tools/ directory


If not, it's possible that you're trying to load all of that stuff but nothing is happening because c5 isn't actually "hooked up".

2) Perhaps you're including too much stuff? You normally don't need to include all of those libraries and models yourself -- if concrete5 loads normally, to add a page you just need to include the collection_types library. Also, I think you have some of the key names wrong in the $pData array -- for example, it's 'name', not 'cName'.

There are probably other ones that aren't right as well, but I don't know off the top of my head. Try paring the whole thing down, just adding a page with just the name. If it works, then go from there and add one more at a time. When it stops working, you know you've hit something that's wrong.

Good luck!

garrycl replied on at Permalink Best Answer Reply
Thanks Jordan :)

I am running it from a file in the "tools" directory of a "package".

However, I have just solved the problem :) :)

What it was is the CollectionType. I was trying to use a top-level page as the CollectionType. However, it needs to be a page type; e.g. "full". So the code is now:
$pData = array('uID' => 12, 'pkgID' => 0, 'cName' => 'Test Five Page', 'cHandle' => 'testfive', 'cDatePublic' => '2011-03-01');
$oPage = Page::getByPath('/professionals');
$ct = CollectionType::getByHandle('full');
$newPage = $oPage->add($ct, $pData);

All the best.

carl101lee replied on at Permalink Reply
Hi All,

I am trying to add a Button to a dashboard form, where you can create a new page. Now I want to be able to Create the Page from a click button, and set the page type and location of the page in the code . But allow the user to input the Name, meta name and all other basic properties for the page?

I wonder If I can use this code to create an add page event and button?
Ps i am on latest version of Concrtet5

<?php function AddingDomainPage()  
   $pData = array('uID' => 12, 'pkgID' => 0, 'cName' => 'Test Five Page', 'cHandle' => 'testfive', 'cDatePublic' => '2011-03-01');
$oPage = Page::getByPath('/more-domains');
$ct = CollectionType::getByHandle('DomainPage');
$newPage = $oPage->add($ct, $pData); 
<div class="CreatePage">
   Create A domain Page  .<input type="button" value="CreatePage" onclick="AddingDomainPage()" /><br/>

When I try and add the helpers/loader form the top I get lots of errors?