This is the documentation for concrete5 version 5.6 and earlier. View Current Documentation

If you have a relatively complex package with:

Themes Attributes PageTypes Blocks Groups SinglePages UserAttributes FileAttributes

Your going to want a way to ensure that every time you run an update on that package any additions to your package components are included... The code below illustrates the basic concept of how we achieved this on quite a large project.

class ExamplePackage extends Package {

    protected $pkgHandle = 'example_package';
    protected $pkgName = 'Example Package';
    protected $pkgDescription = 'Example package - how to install complex packages.';
    protected $appVersionRequired = '5.6.0';
    protected $pkgVersion = '0.1';

    public function install() {
        $pkg = parent::install();
        $this->configurePackage($pkg);
    }

    public function upgrade() {
        $pkg = $this;
        parent::upgrade();
        $this->configurePackage($pkg);
    }

    public function configurePackage($pkg){
        $this->configureThemes($pkg);
        $this->configureAttributes($pkg);
        $this->configurePageTypes($pkg);
        $this->configureBlocks($pkg);
        $this->configureUserGroups($pkg);
        $this->configureSinglePages($pkg);
        $this->configureUserAttributes($pkg);
        $this->configureFileAttributes($pkg);
        $this->overrideCoreSinglePagePackage($pkg);
    }

    public function uninstall() {
        parent::uninstall();
        $this->unOverrideCoreSinglePagePackage($pkg);
    }

}

Each of the configure*() methods essentially just check for the existence of the "component" thats being installed and installs accordingly.

The overrideCoreSinglePagePackage() method is something that updates the core single pages for login, register etc to link them to our package for use with new controllers etc.

Below are some code snippets used the each of the methods above....

FileSets

//create file set for myfiles
if(!(FileSet::getByName('myfiles')) instanceof FileSet){
    FileSet::createAndGetSet('myfiles', FileSet::TYPE_PUBLIC);
}

FileAttribute

//file format tag custom (select attr type) eg. PDF
if(!(FileAttributeKey::getByHandle('format_type') instanceof FileAttributeKey)){
    $args = array(  'akHandle' => 'format_type',
                    'akName' => 'File Type Description' ,
                    'akType' => 'select',
                    'akSelectAllowMultipleValues'=>'0',
                    'akSelectOptionDisplayOrder'=>'alpha_asc',
                    'akIsSearchableIndexed' => '1',
                    'akIsSearchable'=> '1');
    $at = AttributeType::getByHandle('select');
    $fak = FileAttributeKey::add($at, $args, $pkg);
}

User Attribute

Loader::model('user_attributes');
if(!is_object(UserAttributeKey::getByHandle('job_title'))) {
    UserAttributeKey::add(
        'SELECT',array(
            'akHandle' => 'job_title',
            'akName' => t('Job title'),
            'akIsSearchable' => 1,
            'akIsEditable' => 1,
            'uakProfileEdit' => 1,
            'uakProfileEditRequired' => 1,
            'uakRegisterEdit' => 1,
            'uakRegisterEditRequired' => 1,
            'uakProfileDisplay' => 1)
        , $pkg);
}

Themes

if(!is_object(PageTheme::getByHandle('MyTheme'))) PageTheme::add('MyTheme', $pkg);

SingePages

Loader::model('single_page');
$sp = SinglePage::add('/wishlist', $pkg);
if ($sp) {
    $sp->update(array('cName'=> 'Page Name'));
}

PageTypes

if(!is_object(CollectionType::getByHandle('news'))) {
    CollectionType::add(array('ctHandle'=>'news','ctName'=>t('News')),$pkg);
}

Blocks

if(!is_object(BlockType::getByHandle('simple_info'))) BlockType::installBlockTypeFromPackage('simple_info', $pkg);

Collection Attributes

$collection = AttributeKeyCategory::getByHandle('collection');
$collection->setAllowAttributeSets(AttributeKeyCategory::ASET_ALLOW_SINGLE);

$checkboxAttributeType = 'boolean';
$selectAttributeType = 'select';

$collection_attributes = array(
    array('akHandle' => 'is_featured_page',
            'akName' => 'Featured page',
            'akType' => $checkboxAttributeType),
    array('akHandle' => 'persona',
            'akName' => 'Persona' ,
            'akType' => $selectAttributeType)
    );

//Add each of the attrbutes defined
foreach($collection_attributes as $key => $newAttribute){
    $this->addNewAttribute($collection_attributes,$newAttribute,$pkg);
}

private function addNewAttribute($attributeSet,$newAttribute,$pkg){
    $newAttributeHandle = CollectionAttributeKey::getByHandle($newAttribute['akHandle']);
    if(!is_object($newAttributeHandle)){
        $attributeType = AttributeType::getByHandle($newAttribute['akType']);
        $newAttributeHandle = CollectionAttributeKey::add($attributeType,$newAttribute,$pkg)->setAttributeSet($attributeSet); ;
    }
    return $newAttributeHandle;
}

Adding Select Attribute Options

private function addPersonaAttributeSelectOptions()
{
    $personaTypeAttribute = UserAttributeKey::getByHandle('persona');
    $personaOptions = array("Option1","Option2","Option3");
    foreach($personaOptions as $newOption)
    {
        SelectAttributeTypeOption::add($personaTypeAttribute,$newOption);
    }
}

Hopefully that should give you a bit of a head start... Feel free to get in touch if you have any specific questions!

Loading Conversation