官术网_书友最值得收藏!

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:

  1. Open /config/site_post.php in your preferred code editor.
  2. Declare the handle of the new page type to be created.
    $handle = 'page_type_handle';
  3. Declare the array that contains the data for the ctHandle and ctName fields.
    $data = array(
      'ctHandle' => $handle,
      'ctName' => 'Page Type Name'
    );
  4. Check to see if the page type already exists by loading it by the new handle.
    $pageType = CollectionType::getByHandle($handle);
  5. If the page type does not exist, create it.
    if (!$pageType) {
      $newPageType = CollectionType::add($data);
    }
  6. If it does exist, assign the existing page type to the new page type variable.
    else {
      $newPageType = $pageType;
    }
  7. 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
主站蜘蛛池模板: 兴义市| 克什克腾旗| 新建县| 台中县| 松原市| 叶城县| 茌平县| 通道| 南溪县| 玉屏| 塔河县| 横峰县| 祁东县| 钦州市| 大方县| 台山市| 永川市| 秭归县| 武威市| 藁城市| 阿拉尔市| 新巴尔虎左旗| 和政县| 达州市| 崇文区| 黔西县| 新余市| 万山特区| 沈丘县| 集贤县| 天祝| 梓潼县| 嘉定区| 墨竹工卡县| 措美县| 田东县| 宿迁市| 金寨县| 平凉市| 任丘市| 平原县|