Strange code

Permalink
I found this code but I can't understand what it is for
Loader::model('collection_types');
if(!is_object(CollectionType::getByHandle('home')) {
  $data['ctHandle'] = 'home';
  $data['ctName'] = t('Home');
  $hpt = CollectionType::add($data, $pkg);
}

I found this code in this pagehttp://www.concrete5.org/documentation/developers/system/submit-the...
Where I should put this code and for what reason should I do?
Thank you

Fxxxx
 
mkly replied on at Permalink Best Answer Reply
mkly
This code is to check that the page type you are adding(if you are going to add a page type) does not already exist. Maybe with these handles and names it makes more sense.

Loader::model('collection_types');
if(!is_object(CollectionType::getByHandle('my_page_type_handle')) {
  // We wouldn't enter this part unless the above check
  // did not return a CollectionType(page type) object
  $data['ctHandle'] = 'my_page_type_hande';
  $data['ctName'] = t('My Page Type Name');
  // add the page type
  $hpt = CollectionType::add($data, $pkg);
}
codingpenguins replied on at Permalink Reply
This is only if you create a new page type.

Example, all your 'home' would be the page type. This would check the if there is another collectiontype named home. If home is not found then it will add it as a new collectiontype. This is to prevent overriding or collectiontype being used by two different types.
Fxxxx replied on at Permalink Reply
Fxxxx
OK but where i should put this code to create automatically a page type?
I have create a theme with about 15 page type but when I want to try the theme in my concrete5 site I have to add manually page types... Can it be automatically with that code?!?!?!
Can you help me?
Thank you
mkly replied on at Permalink Reply
mkly
You would put that in the install() method of your Package Controller.

Details about halfway down on this page

http://www.concrete5.org/documentation/developers/system/packages/...
Fxxxx replied on at Permalink Reply
Fxxxx
I need some help!
If I want to create a theme I have to create a Controller.php file ?
In this file I have to put some code that install the page type o Blocks that i want?
mkly replied on at Permalink Reply
mkly
If you want to "Package" a theme you need to create a controller.php file. This will also be used to install any blocks or page types or anything else you want to add with your theme "package".
Fxxxx replied on at Permalink Reply
Fxxxx
OK
Can you write the code to install a theme with a page type?
Thank you
mkly replied on at Permalink Reply
mkly
Don't thank me, thank @j12345 as he's already written it all down for you in this howto.

http://www.concrete5.org/documentation/how-tos/designers/packaging-...
Fxxxx replied on at Permalink Reply
Fxxxx
But with this code controller.php file will install all page type?
Shouldn't change some code?