Installing Attributes with a Package
Permalink 1 user found helpful
I have packaged up a theme and would like to install a couple of text & image attributes along with the theme. I am using the code below in my controller.php file and the text attributes install fine, but not the image ones. What am I not doing right?
Loader::model('collection_attributes'); // install attributes $cab1 = CollectionAttributeKey::add('header_image', t('Header Image'), true, null, 'IMAGE'); $cab2 = CollectionAttributeKey::add('job_title', t('Job Title'), true, null, 'TEXT');
Tony, I got my code from the Calendar addon. Here's how it shows how to add a boolean attribute:
I thought I could just change BOOLEAN to IMAGE and that would work but it doesnt. It seems to install the attribute somewhat but it doesnt install the handle or name. See screenshot attached.
$cab3 = CollectionAttributeKey::add('calendar_event_is_all_day', t('Event is All Day'), true, null, 'BOOLEAN');
I thought I could just change BOOLEAN to IMAGE and that would work but it doesnt. It seems to install the attribute somewhat but it doesnt install the handle or name. See screenshot attached.
so if that's not working, then why don't you try it the way i just showed you?
oh, btw, you probably want to use "image_file" or something like that as the attribute type handle.
Was there any final conclusion to this?
I am trying to do the same thing in a theme package. I've tried a couple of different methods with no avail.
Here's my code at the moment:
This is modeled after Tony's suggestion. It would throw an error in the controller and the attribute wouldn't install.
I think it may have to do with "if( !is_object($displayPageTitleDescription)"
What is that object supposed to be or do? Is it supposed to correlate with the attribute in some way?
Oddly enough I installed a test attribute after having tried to install this one, and it went through fine—I had forgotten to change $displayPageTitleDescription to something else—like $testAttribute.
I am trying to do the same thing in a theme package. I've tried a couple of different methods with no avail.
Here's my code at the moment:
Loader::model('collection_attributes'); $boolt = AttributeType::getByHandle('boolean'); $displayPageTitleDescription=CollectionAttributeKey::getByHandle('display_page_title_and_description'); if( !is_object($displayPageTitleDescription) ) CollectionAttributeKey::add($boolt, array('akHandle' => 'display_page_title_and_description', 'akName' => t('Display Page Title and Description'), 'akIsSearchable' => false, 'akCheckedByDefault' => true));
This is modeled after Tony's suggestion. It would throw an error in the controller and the attribute wouldn't install.
I think it may have to do with "if( !is_object($displayPageTitleDescription)"
What is that object supposed to be or do? Is it supposed to correlate with the attribute in some way?
Oddly enough I installed a test attribute after having tried to install this one, and it went through fine—I had forgotten to change $displayPageTitleDescription to something else—like $testAttribute.
Managed to figure it out by myself. Here is what you'd need to install a custom attribute (a pre-built one, like Text or Image/File, not one you built yourself).
Full code, starting from the install() function:
Place your own attribute handles and names as appropriate. Don't forget to change the variable names too (like $testAttribute5). The two attributes being installed there are a boolean and an image/file one, respectively.
The different attribute object names are listed in concrete/controllers/install.php. This is what you'd be looking for:
I'm not sure about the handles, but they should be easy enough to figure out. IE $tt is "text", $ift is "image_file", $boolt is "boolean", etc.
NOTE: I have found that uninstalling a package that installs attributes this way will not uninstall the attributes themselves. The attributes would have to be manually deleted (this isn't a problem—it can even be a good thing—it's, just something I noticed)
Full code, starting from the install() function:
public function install() { $pkg = parent::install(); Loader::model('collection_attributes'); // a boolean attribute $boolt = AttributeType::getByHandle('boolean'); $testAttribute3=CollectionAttributeKey::getByHandle('test_attribute3'); if( !is_object($testAttribute3) ) CollectionAttributeKey::add($boolt, array('akHandle' => 'test_attribute3', 'akName' => t('Test Attribute3'), 'akIsSearchable' => false, 'akCheckedByDefault' => true)); // an image/file attribute $ift = AttributeType::getByHandle('image_file'); $testAttribute5=CollectionAttributeKey::getByHandle('test_attribute5'); if( !is_object($testAttribute5) ) CollectionAttributeKey::add($ift, array('akHandle' => 'test_attribute5', 'akName' => t('Test Attribute5'), 'akIsSearchable' => false, 'akCheckedByDefault' => true)); }
Place your own attribute handles and names as appropriate. Don't forget to change the variable names too (like $testAttribute5). The two attributes being installed there are a boolean and an image/file one, respectively.
The different attribute object names are listed in concrete/controllers/install.php. This is what you'd be looking for:
// from the core's install.php file - these attribute names apply to pages. $cakc->associateAttributeKeyType($tt); $cakc->associateAttributeKeyType($textareat); $cakc->associateAttributeKeyType($boolt); $cakc->associateAttributeKeyType($dtt); $cakc->associateAttributeKeyType($ift); $cakc->associateAttributeKeyType($nt); $cakc->associateAttributeKeyType($rt); $cakc->associateAttributeKeyType($st);
I'm not sure about the handles, but they should be easy enough to figure out. IE $tt is "text", $ift is "image_file", $boolt is "boolean", etc.
NOTE: I have found that uninstalling a package that installs attributes this way will not uninstall the attributes themselves. The attributes would have to be manually deleted (this isn't a problem—it can even be a good thing—it's, just something I noticed)
To further clarify Proteus's response, here is the list of built-in attribute type handles:
'address'
'boolean'
'date_time'
'image_file'
'number'
'rating'
'select'
'text'
'textarea'
(those variables like $tt, $ift, etc. aren't going to do you any good because those are just what the install script has assigned for itself -- they are not automatically available to you in your own scripts).
'address'
'boolean'
'date_time'
'image_file'
'number'
'rating'
'select'
'text'
'textarea'
(those variables like $tt, $ift, etc. aren't going to do you any good because those are just what the install script has assigned for itself -- they are not automatically available to you in your own scripts).
Have just tried this and it seems to work nicely :o)
You should maybe consider adding the $pkg variable onto the end of the ::add calls so that when you uninstall the package the attribute gets removed also.
e.g.
You should maybe consider adding the $pkg variable onto the end of the ::add calls so that when you uninstall the package the attribute gets removed also.
e.g.
CollectionAttributeKey::add($boolt, array('akHandle' => 'test_attribute3', 'akName' => t('Test Attribute3'), 'akIsSearchable' => false, 'akCheckedByDefault' => true), $pkg);
but it does look like the approach you're taking is a bit different than what i've been using. Here's an example of installing a boolean user attribute: