Pages Basics
Working with Pages in Concrete CMS
The Concrete\Core\Page\Page object is central in Concrete CMS. Pages are primary entry points, and they can hold various custom attributes.
Accessing the Current Page
In a Page Template:
The current page object is automatically present as: $c
(refers to the "current page").
In a Block Template:
The $c
object might not be directly available. To get the current page, use:
$c = \Page::getCurrentPage();
Fetching a Page by Location or ID
To get a page by its location:
$c = \Page::getByPath('/path/to/page', 'ACTIVE');
And by its ID:
$c = \Page::getByID(1, 'ACTIVE'); // This fetches the home page.
Accessing the Latest Page Version
The above methods usually get the approved version of the page. For the most recent version, omit the version specifier:
$c = \Page::getByPath('/path/to/page');
$c = \Page::getByID(1);
Accessing Page Details
Standard Properties
Here's how to access some default page properties:
- Name:
$c->getCollectionName()
- Description:
$c->getCollectionDescription()
- Creation Date:
$c->getCollectionDateAdded()
- Public Change Date:
$c->getCollectionDatePublic()
- Owner's User ID:
$c->getCollectionUserID()
Custom Attributes
Fetch a custom attribute:
$value = $c->getAttribute('attribute_handle');
For example:
- Exclude from Navigation (Boolean):
$c->getAttribute('exclude_nav')
- Thumbnail (Image/File):
$c->getAttribute('thumbnail')
For display-friendly values:
$value = $c->getAttribute('thumbnail', 'display');
Displaying Tags
To list and link tags from a page:
<?php
$tags = $c->getAttribute('tags');
if ($tags && count($tags)) { ?>
<div class="tags">
<div class="title"><span>Tags</span></div>
<ul>
<? foreach($tags as $tag) { ?>
<li><a href="<?=URL::page($blog, 'tag', strtolower($tag))?>"><?=$tag?></a></li>
<? } ?>
</ul>
</div>
<? }
?>
Additional Functions
- Check Edit Mode:
if ($c->isEditMode()) { }
- Get All Blocks on a Page:
$c->getBlocks()
- Get Blocks from a Specific Area:
$c->getBlocks('Area name')
- Get Block IDs from an Area:
$c->getBlockIDs('Area name')
- Access an Area:
$c->getArea('Area name')
Page API Reference
For more on the page object, see the API documentation.
Page Type Controllers
Controllers can be added to page types. For a page of a specific type, its controller's view()
method runs automatically. This lets the controller pass data to the view.
Identifying Page Type
For example, if working with the "Blog Entry" page type, its handle is blog_entry
.
Setting up the Controller
- Make a file at
application/controllers/page_types/blog_entry.php
. In this file, define a controller class named after the page type (in CamelCase) and in the
\Concrete\Controller\PageType
namespace:namespace Application\Controller\PageType; use Concrete\Core\Page\Controller\PageTypeController; class BlogEntry extends PageTypeController { public function view() { // use `$this->set('var_name', <var_value>);` to make available in view } }
Note
Even though PageTypeController
currently just extends PageController
, use it. Future updates might add essential methods to PageTypeController
.
Conclusion
With the above steps, the view()
method in the controller will activate automatically, and its data will be accessible in the page template.
Changing Page Title via Controller
Use the SEO Helper service object to adjust a page's title.
$seo = app('helper/seo');
This gives you the \Concrete\Core\Html\Service\Seo object. It has methods:
- setSiteName
- addTitleSegment
- setTitleFormat
- setTitleSegmentSeparator
With this, you can alter your site's title. For example, if you render a custom view for a single page:
public function view_album($albumID = null)
{
$selectedAlbum = \Foo\Media\Album::getByID($albumID);
$this->set('albumID', $albumID);
$this->set('album', $selectedAlbum);
$this->render('/media/albums');
// Set page title
$seo = app("helper/seo");
$seo->addTitleSegment('Albums');
}
In a block view, you can adjust the title too. For instance, the Page List block updates the title based on an active topic:
public function action_filter_by_topic($treeNodeID = false, $topic = false)
{
if ($treeNodeID) {
$this->list->filterByTopic(intval($treeNodeID));
$topicObj = Topic::getByID(intval($treeNodeID));
if (is_object($topicObj)) {
$seo = app('helper/seo');
$seo->addTitleSegment($topicObj->getTreeNodeDisplayName());
}
}
$this->view();
}
Note: These changes are runtime-only. The original page's name remains the same in records.