Hello Community, in this tutorial I will show a technique to assign custom attributes to a page type and make them default for that page type.
This is useful for theme developers and I am using a theme packaging example here. So the goal is to make available for the end user custom attributes that you as developer might have created for a specific page type without depending on the user to add a new attribute to the page type.
So from default instalation you will notice that the pagetype (page) has by default the following attributes:
- Meta Title (id=1)
- Exclude From Nav(id=2)
- Meta Description(id=3)
- Meta Keywords(id=4)
We want to add it to all our pagetypes plus our custom attributes as well. This is how you can do it.
In your package controller load all required models.
Loader::model('collection_types');
Loader::model('collection_attributes');
Loader::model('attribute/categories/collection');
In your controller add a new pagetype example (project):
// install page types
$theme = CollectionType::getByHandle('project');
if(!$theme || !intval($theme->getCollectionTypeID())){
$theme = CollectionType::add(array('ctHandle'=>'project','ctName'=>t('Project Page')),$pkg);
}
Now lets add the custom attributes for project pagetype:
$eaku = AttributeKeyCategory::getByHandle('collection');
$eaku->setAllowAttributeSets(AttributeKeyCategory::ASET_ALLOW_SINGLE);
$themeSet = $eaku->addSet('built_in',t('Categories Atributes'),$pkg);
// install collection attributes
$ak1=CollectionAttributeKey::add($image_file,array('akHandle'=>'project_image','akName'=>t('Project Image'),'akIsSearchable'=>false),$pkg)->setAttributeSet($themeSet);
Note that for the sake of organization I have created a New Set of attributes called built in to keep everything organized and after that I create Project Image attribute.
Now we need to assign our attributes to our pagetype .
$pageType = CollectionType::getByHandle('project');
$ak= CollectionAttributeKey::getByHandle('project_image');
$pageType->assignCollectionAttribute($ak);
Do this for each attribute/page type combination, and the attributes will show up by default when editing the page properties for pages of that type. Many thanks to Andrew for rectifiying this tutorial and Mnkras that gave a correct anwser when I asked in the forums.