adding attributes on block install?
Permalink
I was thinking I could just run a sql query on install but wondered if there already was something in place that i should use.
packages (sometimes called applications) are more of what you're looking for.. can include new page types, themes.. etc..
http://www.concrete5.org/index.php?cID=2943...
I would like to be able to create a new page and automatically assign an attribute to that page, is that possible?
If so, then try this code (put it in the "install()" function in your package's controller.php file). I haven't tested it but I think it should work:
//Assumes you've already called $pkg = parent::install(); Loader::model('collection_types'); $pageType = CollectionType::getByHandle('page_type_handle'); //<--replace this with the handle for the page type you want this new page to be (left_sidebar, right_sidebar, full, etc.) $parentPage = Page::getByID(1); //<--assumes you want to create a top-level page (under home page). If you want the new page to appear elsewhere, put in a different id or use Page::getByPath('/path/to/parent/page') $newPage = $parentPage->add($pageType, array('name' => 'My New Page Title')); Loader::model('collection_attributes'); $at = AttributeType::getByHandle('text'); //<--replace this with the kind of attribute you want (address, boolean, date_time, iamge_file, number, rating, select, text, textarea, etc.) $data = array( 'akHandle' => 'my_attribute_handle', 'akName' => 'my_attribute_name', 'akIsSearchable' => true, ); $ak = CollectionAttributeKey::add($at, $data); $newPage->setAttribute($ak, 'attribute value');
Thanks for your comments, I sort of mashed something together from various other threads I could find and ended up with this.
On the update and add, functions
$p = Page::getByID($this->post('cID')); $parent = Page::getByID($this->post('cParentID')); $ct = CollectionType::getByID($this->ctID); $data = array('ctID' =>$ct->getCollectionTypeID(), 'cDescription' => $this->post('clientsDescription'), 'cName' => $this->post('clientTitle'), 'cDatePublic' => Loader::helper('form/date_time')->translate('clientsDate')); $p->update($data); if($this->post('fID')>0){ $this->fID = $this->post('fID'); $fobj = $this->getFileObject(); $p->setAttribute('page_thumbnail',$fobj); } $p->setAttribute('client_entry',intval(1));