For the package I’m developing I have a single_page that I’m adding as one of the key pages for displaying and managing the information. The information is stored as pages and pages attributes, so this main page will have a page_list block to display these. The problem I’d been trying to solve is how to have the package installer add the page_list block during the install as otherwise the user needs to do this – less than ideal 🙁

After some hunting around I finally settled on the following code to add the block, inspired by the code in the importer for content that Concrete 5 uses when you install it.

The key pieces of information you need are

  • the collection id of the pagetype ($pagetype) you wish to list in the page_list block
  • all the pages added are children of a particular page, so I need the collection id of the page ($sp)
  • the name of the area defined on the single_page (‘single_page_area’)
  • an array with the required parameters for the block type being created ($data) which in this case is a page_list

 $sp = Page::getByPath('/my/single_page');  
 $pagetype = CollectionType::getByHandle('mypagetype');  
 $data = array('num' => 25,  
 'ctID' => $pagetype->getCollectionTypeID(),  
 'displayFeaturedOnly' => 0,  
 'displayAliases' => 0,  
 'paginate' => 1,  
 'cParentID' => $sp->cID,  
 'includeAllDescendents' => 0,  
 'orderBy' => 'alpha_asc',  
 'rss' => 0,  
 'truncateSummaries' => 0,  
 'truncateChars' => 128  

 $bt = BlockType::getByHandle('page_list');  
 $b = $sp->addBlock($bt, 'single_page_area', $data);  
 $b->updateBlockInformation(array('bName' => 'myblock_name',  
 'bFilename' => null));

With this code in place the installer creates the block and attaches it correctly, so once the pages are created they appear automatically in the listing – all with a minimum of fuss for anyone using the package 🙂