How to build custom attributes from scratch?

Permalink 1 user found helpful
Hey guys!
I am trying to develope custom attributes from scratch. I have read and completed the how-to written by frz(http://www.concrete5.org/documentation/how-tos/developers/create-a-custom-attribute-based-on-the-address-type/) and RadiantWeb(http://www.concrete5.org/documentation/how-tos/developers/custom-attribute-types/). I have read the corresponding online documentation. Still, there are a lot of things, that I can not seem to figure out. For example: what is the minimum set of methods that need to be implemented inside the controller for the custom attribute type. I understand, that this depends on the complexity of the attribute type. But how can this dependency relation be described? I mean: in the built-in Address attribute, a load() call is made in side the form() method, but in frz's tutorial, it is omitted. Why? I have a lot more questions like these, if there would be someone kind enough to help me out on this topic, who has a deeper understanding of attributes. If there is a more thorough tutorial on this, that would be really nice, but I could not find anything, apart from the above two. :( Thanks in advance!

 
RadiantWeb replied on at Permalink Best Answer Reply
RadiantWeb
I believe the minimum is:

form
type_form
searchForm
getValue
saveForm
saveValue (which can simply forward to $this->saveForm() )
deleteValue
saveKey
deleteKey

Typically, any "form" method will call a load() function, and load() simply calls in that attributes settings table and then sets pertinent global vars for that attribute type.

Each attribute should have two tables with the following minimum requirements:

atTypeName table (prefixed with at - followed by camelcased type name )
atTypeNameSettings (add "Settings" to the end)

the Type table should minimally have an "avID" integer field as a key, and then a second "value" field. char or int.

The settings table should minimally have an "akID" integer field.

The Attribute Type's saveKey method saves to settings. Load method loads settings into context. And deleteKey removes the key and key settings.

It's Form methods call the load() to load the settings, and then also call the getValue to load any saved data.

You can also have multiple form types and call them from a value. From an Attribute Key and Value object you can do the following:

$aValue = $Object->getAttributeValueObject($ak);
$ak->render('custom_form_name',$aValue);


if you then have a corresponding custom_form_name() method with form elements created in it, this then would be the form displayed. You can also simply have the method with nothing but the load() function, and then include a custom_form_name.php file in your attribute type and that file then would be the form loaded into context.


Hope this helps. Attributes are extremely powerful and useful. And this is just scratching the surface of some of the things you can do with them.

Blessings,
ChadStrat
szucslaszlo replied on at Permalink Reply
I did not fully understand everything, but knowing which methods are a minimum is a good starting point I guess! Thank you so far!
szucslaszlo replied on at Permalink Reply
Hey RadiantWeb! Thank you for your help so far. I think I am getting the feel of all this, but I still don't understand a few things.

I am trying to implement the form() function. Specifically: I want to pass some data to form.php, using the set() method. I'm looking in the built-in attribute files for hints on how to properly do things, and I got confused concerning attribute values. As far as I can see, some properties just pop into existence. Take for example $this->attributeValue. I don't see it declared anywhere, nor can I find where it gets initialized. Same thing applies to $this->attributeKey. I don't even understand how does this fit into this MVC architecture. Where do these properties get their values from and how does $this gets a reference to an actual attribute type object?

I look at address.php:form(), the controller for the Address attribute type. It says:
$value = $this->getAttributeValue()->getValue();


What kind of object does getAttributeValue() and getValue() return? Does this relate to the attribute type controller's getValue() method?