Generally, the architecture behind concrete5 is

  • Blocks are the smallest amount of visible content available.
  • Areas contain these blocks.
  • Areas are contained within Collection Versions
  • Collection Attributes are assigned to Collection Versions
  • Collection Versions are contained within Collections
  • Pages extend Collections, turning Collections into something viewable from the front-end.
  • Pages can either be of a certain page type, or a one-off page (single page).

This is why the currently rendered page is typically available as the $c object - it was originally referred to as a Collection object. In practice, the line between Collection and Page is very fuzzy, to the point where developers would be well served by working simply with Page objects, which extend the Collection object anyway.

Working with the current page

Throughout your code, if you wish to grab the object for the currently rendered page, simply execute this code:

$page = Page::getCurrentPage();

This will return the currently rendered page's object.

Adding a page of a certain type under another page

$parentPage = Page::getByPath("/sample");
$data = array(
        'name' => "sample2",
        'cHandle' => "sample2handle",
    'cDescription' => "Add page test."           
);
$pt = CollectionType::getByHandle("page_type_handle");
$newPage = $parentPage->add($pt,$data);

 

Getting a Page Object by ID or Path

$page = Page::getByPath('/path/to/page', $version = 'RECENT');

Returns the current page based on its page path. $version determines which CollectionVersion object to load within the Page object. Valid values are

  • 'recent' - loads the recent version
  • 'active' - loads the currently approved, active version
  • a numerical ID - loads the version by its $cvID

$page = Page::getByID($pageID, $version = 'RECENT');

Same as getByPath, but loads based on the numerical $cID.

Page Object Methods

While the Page (and Collection) objects have a lot of methods, you probably will only need to work with a few:

Reading Data

$page->getCollectionID()

Returns the page's ID.

$page->getCollectionUserID()

Returns the user ID of the page (typically the initial creator of the page, or page's owner if it has been changed through the Page Properties overlay.)

$page->getCollectionHandle()

Returns the page's handle.

$page->getCollectionTypeID()

Returns the ID of the page's collection type.

$page->getCollectionTypeHandle()

Returns the handle of the page's collection type.

$pageTheme = $page->getCollectionThemeObject()

Returns the PageTheme object of the page's current theme.

$page->getCollectionName()

Returns the name of the current page. Queries the currently loaded version object for this information.

$page->getCollectionDescription()

Returns the description of the current page. Queries the currently loaded version object for this information.

$page->getCollectionDatePublic($dateFormat = null, $type = 'system')

Gets the public date for a page. $dateFormat is a PHP date() string. $type may also be 'user' if ENABLE_USER_TIMEZONES is set to true. (Note: this date is the date changeable through the properties overlay.)

$page->getCollectionParentID()

Gets the ID of this page's parent page. Every page has a parent, with the exception of the home page and master collections.

$page->isMasterCollection()

Returns true if the currently accessed page is a master page (accessed through Dashboard > Pages > Page Types)

$page->getCollectionDisplayOrder()

Gets the current sitemap display order of a page.

$page->getNumChildren()

Gets the number of children directly beneath a page.

$page->getFirstChild($sortColumn = 'cDiaplyOrder asc')

Returns the first child of the current page, or null if there is no child.

$page->getAttribute($akHandle)

Returns the display value of an attribute when passed an attribute key handle.

Adding/Updating Pages

$page = Page::add(CollectionType $pageType, $data)

Adds a new page of type $pageType, setting the various provided fields in the $data array. This array may contain:

  • "uID": User ID of the page's owner
  • "pkgID": Package ID the page belongs to
  • "cName": The name of the page
  • "cHandle": The handle of the page as used in the path
  • "cDatePublic": The date assigned to the page

$page->update($data)

Updates a page, and takes the same $data array as above.

$page->move(Page $newParent)

Moves a page to a new location in the site.

$page->duplicate(Page $newParent)

Copies a page to a new location in the site.

$page->delete()

Deletes a page, INCLUDING ALL CHILDREN.

$page->setTheme(PageTheme $pt)

Sets the current theme of a page.

$page->setAttribute($ak, $value)

Sets an attribute against a page. $ak may either be an instance of the CollectionAttributeKey object, or a Collection Attribute handle.

$page->clearAttribute($ak)

Clears an attribute against a page. $ak may either be an instance of the CollectionAttributeKey object, or a Collection Attribute handle.

Working with Blocks

$page->addBlock(BlockType $bt, Area $a, $data)

Adds a block of a certain type to a specific area, providing a $data array (which is then passed to the block's controller save() function.) For more information on this function, consult the developer documentation on blocks.

$blocks = $page->getBlocks($areaHandle = false)

Returns an array of generic Block objects found on a page. If $areaHandle is provided, only those blocks in that area will be passed back.


Recent Discussions on this Topic

Adding a page to the Sitemap

Where does the page get stored when adding a page to the site map? URLs look like: /index.php/profile/showmyprofile/ Which is a physical path but no file is found.

creating a new version programmatically

hej! how can i create a new version of a page by updating its blocks or itself (from within source code)? i have a single page form which creates or updates a page. (all users from a certain group have one such a page, kinda similar to a profile pag…

Page Size

How do you increase the size of a page so it takes up more of the screen. Seems like all the pages are narrow and I would like my pages wider.

Cannot find my custom page attributes in controller

Hi, I have set up a controller, passed it a page(collection)ID and successfully loaded the page object, which contains the usual default stuff- title, version, url etc. I have some custom attributes I have created, however the attributes section of th…

Need one collection/page with two views or page types

Sorry if my terminology is off here... I want to have one page with about 30 attributes, and two different page types to display that data in very different ways. How can I do this? I was imagining programatically switching the page type with a con…