Page Defaults / API

Permalink 1 user found helpful
Couldn't find anything about it, so hopefully anyone can help me:

In the install.php, let's say I want to add content to the sidebar of the home page, I can do it like this:

//sidebar content
$bt = BlockType::getByHandle('content');
$data = array();
$data['uID'] = $this->installData['USER_SUPER_ID'];
$data['content'] = t('Text on my sidebar...');
$home->addBlock($bt, "Sidebar", $data);

But I would like to not only add this block to one page, I want it to be the default on the added page type and also add it to any other existing page sharing the same page type (e.g left_sidebar). How can I do this?

 
Mnkras replied on at Permalink Reply
Mnkras
just do it on the page-defaults page, or you can hard code things aswell
Kiesel replied on at Permalink Reply
I want to prepare an installer so new websites have already a specific content over each page.

Hard coding would be a bad idea cause the Editor User most likely wants to change it.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
Is your installer adding this new page type or is it working with a page type that already exists in the system?

If it's adding the new page type, do this:
$pkg = parent::install();
Loader::model('collection_types');
$data = array(
   'ctHandle' => 'my_pagetype_handle',
   'ctName' => t('My Page Type Name'),
   'ctIcon' => 'main.png',
);
$myNewPageType = CollectionType::add($data, $pkg);
$myNewPageDefaults = $myNewPageType->getMasterTemplate();
//sidebar content
$bt = BlockType::getByHandle('content');
$data = array();
$data['uID'] = $this->installData['USER_SUPER_ID']; //<-- NOTE that this isn't necessary because the block will be installed under the current user's ID if you don't provide your own
$data['content'] = t('Text on my sidebar...');
$myNewPageDefaults->addBlock($bt, "Sidebar", $data);


If you want to add it to an existing page type, do this:
Loader::model('collection_types');
$myPageType = CollectionType::getByHandle('my_pagetype_handle');
$myPageDefaults = $myPageType->getMasterTemplate();
//sidebar content
//...same as above...
Kiesel replied on at Permalink Reply
Thank you, that was exactly what I was looking for!