Installing Custom Attribute Types with Package

Permalink 1 user found helpful
I'm creating a package and have the need for a custom attribute type, which I've created called 'Contacts'.

I'm able to install it by adding the files into my_site_folder/models/attribute/types/contact

Everything works splendidly. Now, however, I'd like to wrap this new attribute type up into a package and allow sites that install this package to get this new attribute type installed as well. Will this work from the my_site_folder/packages/models/attribute/types/contact?

And, what is the method for installing a new attribute type from the package controller?

Thanks in advance. I've searched around the forums but did not see an answer to this. I apologize if it is out there already.

jeramy
 
synlag replied on at Permalink Reply
synlag
Hi,

this could hlp

http://www.concrete5.org/documentation/how-tos/create-a-custom-attr...

else ecommerce shows a way how this can be solved.
jeramy replied on at Permalink Reply
jeramy
That link did help with creating the custom attribute, but then you get to the bottom and it only explains how to add it manually to your site. I'd like to do this automatically with the install of a custom package that I'm creating.
synlag replied on at Permalink Reply
synlag
Take a look at the single page install and the controller, it also shows how this can be done.
jeramy replied on at Permalink Reply
jeramy
This almost gets me there:
$cakc = AttributeKeyCategory::getByHandle('collection');
$ct = AttributeType::add('contact', t('Contacts'),'news_room');
$cakc->associateAttributeKeyType($ct);


However, this only works if I put the contact type folder in my_site_folder/models/attribute/types .

So I guess I should have the controller install function copy this folder and needed files into the sites models folder, then add the Attribute Type? It seems like that would work, but seems clunky somehow.
andrew replied on at Permalink Reply
andrew
You're very close. Instead of:

$ct = AttributeType::add('contact', t('Contacts'),'news_room');

Do this:

$ct = AttributeType::add('contact', t('Contacts'),'news_room', $pkg);

Where $pkg is

$pkg = parent::install();

from inside the install() method of your package's controller.
jeramy replied on at Permalink Reply
jeramy
Thanks Andrew!

At first, it did not work, but I changed this line
$ct = AttributeType::add('contact', t('Contacts'),'news_room', $pkg);


to

$ct = AttributeType::add('contact', t('Contacts'), $pkg);


Now it works beautifully!
jeramy replied on at Permalink Reply
jeramy
So this all seemed to work until I created a page and added the custom attributes to it. Now I get this error:

Warning: Loader::require_once(/ip/meddev/concrete/config/../models/attribute/types/contact/controller.php) [function.Loader-require-once]: failed to open stream: No such file or directory in /ip/meddev/concrete/libraries/loader.php on line 55

Fatal error: Loader::require_once() [function.require]: Failed opening required '/ip/meddev/concrete/config/../models/attribute/types/contact/controller.php' (include_path='.:/usr/local/php5.2.4/lib/php:/ip/meddev/www/communications/libraries/3rdparty:/ip/meddev/concrete/config/../libraries/3rdparty') in /ip/meddev/concrete/libraries/loader.php on line 55

I assume that the model loader needs placed somewhere, but cannot for the life of me figure out where:

Loader::model('attribute/types/contact','news_room');

Thanks in advance!
nteaviation replied on at Permalink Reply
nteaviation
I believe our path is incorrect. You may try a path something like BASE_DIR.'/models/attributes/types/contacts','news_room'. I not sure exactly where you are trying to reference.

I'm not sure of the CONSTANT name, but someone here will "probably chime in" :)
andrew replied on at Permalink Best Answer Reply
andrew
Unfortunately I think you may be stuck. I just had this conversation with Tony and this is a core bug. We've got it fixed in svn but the problem is basically that for custom attribute types to work with pages, they need to be registered properly in startup/autoload.php, and they weren't. At least packaged one's weren't. We didn't notice this before because the objects that the custom attributes apply to in the eCommerce package aren't cached.

To fix this on your local install, open concrete/startup/autoload.php and change

Loader::model('attribute/types/' . $handle . '/controller');

to

$at = AttributeType::getByHandle($handle);

That should autoload the class properly. This will be fixed in a point release in the next few weeks.
jeramy replied on at Permalink Reply
jeramy
Why do I love this CMS? The community here ROCKS!

Thanks Andrew. That will work. This package is not heading to the marketplace, just internal use. Our school department sites share a core, so this change was easy and works.
andrew replied on at Permalink Reply
andrew
Ah excellent. It bums me out when I have to tell people that their public add-on is going to have to wait for us to fix a core bug. Good to hear that this has rectified the issue.
jkns replied on at Permalink Reply
Hi Andrew,

Has this been fixed for 5.6.0.2? If so, how do you do this now? Or do you not recommend adding Custom Attributes to Packages?

Kind regards.
bigearl replied on at Permalink Reply
bigearl
Bump?

Andrew, I'd love to install Custom Attributes from a Package, is this possible? Is there a roadmap for this feature?

Thanks! I know you guys are busy, but would be good to know if I should move on...
OiseauVernal replied on at Permalink Reply
I know it's been a while, but if you're still looking for how to do this, check this out:

http://www.concrete5.org/marketplace/addons/basic-package-for-devel...
elpado replied on at Permalink Reply
elpado
How it works today in Concrete 5.7.4?

The package seems outdated and the posted link is dead ..
dkmattew replied on at Permalink Reply
I want to know too....
razorcommerce replied on at Permalink Reply
razorcommerce
In our Razor Commerce package controller see the method add_attribute_types() in the file: https://github.com/RazorCommerce/razor-commerce/blob/master/razor/co...

It can be as simple as this:

$at_price = AttributeType::add('price', 'Price', $this->pkg);


However we found we need to check for it already being installed (thought normally it should be uninstalled automatically). It will cause a fatal error if you try to install using existing handle, so think about naming your attributes to avoid conflicts and/or doing an object check.

$at_price = AttributeType::getByHandle('price');
if ( !is_object( $at_price )) {
  $at_price = AttributeType::add('price', 'Price', $this->pkg);
}


Should mention you might normally have $pkg, we have $this->pkg because we store the Package object instead of passing it around the package installer.
razorcommerce replied on at Permalink Reply
razorcommerce
It looked like mainly you want to know how to install a whole new attribute, but if your next question is how to make AttributeKeys, instances of that new Attribute, we abstracted that into a class you can find here: https://github.com/RazorCommerce/razor-commerce/blob/master/razor/sr...