- concrete5 Cookbook
- David Strack
- 392字
- 2021-08-13 16:15:56
Creating a page type
You can easily add new page types through the concrete5 user interface, but there are some situations that can require you to create page types dynamically using PHP code.
Getting ready
First, you will need to create an associative array that contains the data of the page type that you are creating.
The array can contain the following fields:

How to do it...
We will create a page type using only the required fields: ctHandle
and ctName
. The steps are as follows:
- Open
/config/site_post.php
in your preferred code editor. - Declare the handle of the new page type to be created.
$handle = 'page_type_handle';
- Declare the array that contains the data for the
ctHandle
andctName
fields.$data = array( 'ctHandle' => $handle, 'ctName' => 'Page Type Name' );
- Check to see if the page type already exists by loading it by the new handle.
$pageType = CollectionType::getByHandle($handle);
- If the page type does not exist, create it.
if (!$pageType) { $newPageType = CollectionType::add($data); }
- If it does exist, assign the existing page type to the new page type variable.
else { $newPageType = $pageType; }
- Dump the new page type variable to ensure that it worked.
my_debug($newPageType);
How it works...
concrete5 will take the data array that you pass in and use it to create a new CollectionType
record in the database. concrete5 will also automatically generate a Master Collection page in the database. The Master Collection is a template page that concrete5 will use as base for all pages of that type. Any blocks, attributes, or other data assigned to the Master Collection will automatically be added to any new pages created with that page type.
There's more...
If you are creating this page type in a custom package, you will need to pass in your package object to the add()
function, so concrete5 knows that this page type belongs to your package. That way, when users uninstall your package, they will have the option of removing all of the page types that it created.
Assuming that you know the handle of your package, use the following code to create a page type with it:
$data = array( 'ctHandle' => $handle, 'ctName' => 'Page Type Name' ); $pkg = Package::getByHandle('my_package'); $newPageType = CollectionType::add($data, $pkg);
See also
- The Creating custom add-on package recipe
- The Updating a page type recipe
- Mastering Ext JS(Second Edition)
- Facebook Application Development with Graph API Cookbook
- 騰訊iOS測試實(shí)踐
- PaaS程序設(shè)計(jì)
- Learning Neo4j 3.x(Second Edition)
- C語言程序設(shè)計(jì)
- Linux命令行與shell腳本編程大全(第4版)
- Serverless架構(gòu)
- BIM概論及Revit精講
- Angular開發(fā)入門與實(shí)戰(zhàn)
- 計(jì)算機(jī)應(yīng)用基礎(chǔ)項(xiàng)目化教程
- RocketMQ實(shí)戰(zhàn)與原理解析
- Scala Functional Programming Patterns
- 零基礎(chǔ)輕松學(xué)C++:青少年趣味編程(全彩版)
- INSTANT LESS CSS Preprocessor How-to