Acces to controller from a attribute type
Permalink
Hi,
I'm working on my free package 'fileset_attribute' to improve it a bit. I would add some function int the controller to help people to work with. in this way i will add for example the function 'getFileListObjectValue' to get a files list object from the fileset.
BUT.. How to access to this special function from a View? It seems that before 5.6 we has able to specify a special function to get value, but now ? It's semmes that is impossible ?
I've tryed some tricks without result..
Thanks a lot !
Seb
I'm working on my free package 'fileset_attribute' to improve it a bit. I would add some function int the controller to help people to work with. in this way i will add for example the function 'getFileListObjectValue' to get a files list object from the fileset.
BUT.. How to access to this special function from a View? It seems that before 5.6 we has able to specify a special function to get value, but now ? It's semmes that is impossible ?
I've tryed some tricks without result..
$c = Page::getCurrentPage(); $cc = Collection::getByID($c->cID); $fs = $c->getAttributeValueObject('fileset'); // Fatal error: Call to a member function getAttributeKeyID() on a non-object Loader::model('attribute/categories/collection'); $attribs = $cc->vObj->getAttribute('fileset',$c, 'getFileListObjectValue'); // string(1) "1" $ak = CollectionAttributeKey::getByHandle('fileset'); if (is_object($ak)) { $av = $c->getAttributeValueObject($ak); if (is_object($av)) { $fr = $av->getFileListObjectValue('getFileListObjectValue'); // Fatal error } } // And this work but the controller can't acces to the attribute value .. $ak = CollectionAttributeKey::getByHandle('fileset'); $satc = new FilesetAttributeTypeController(AttributeType::getByHandle('fileset'));
Viewing 15 lines of 17 lines. View entire code block.
Thanks a lot !
Seb
Like you, I would have expected the special function method to work. Could it just be a case of the override cache?
I may not be fully understanding your question, but I can suggest a different way to set up the function.
I'm no real expert on attribute types, but I did build one recently where I wanted to be able to return the stored data in different formats (as HTML, as a raw array of data, etc), I'm pretty sure this is the same sort of problem you are trying to solve.
When you retrieve an attribute from a page object, the basic way to do this is:
$c->getAttribute('fileset');
This is going to call the function getValue() on your attribute, which really is supposed to return the raw data. With your attribute Seb, you haven't defined a getValue() function, it just falls back to the one in DefaultAttributeTypeController. (which works perfectly fine).
What I found out though, is that getAttribute can be passed a 'display mode' as a second argument. When you do this, concrete uses this second argument to work out the name of the function to call on the attribute.
So if you do this:
$c->getAttribute('my_attribute', 'display');
It's going to call the function getDisplayValue() on the attribute. Or if you called:
$c->getAttribute('my_attribute', 'Array');
It's going to call getArrayValue() on the attribute.
So in your case, maybe you need to call:
So it calls your getFileListObjectValue() function of your attribute. That function should be able to do whatever is necessary to build the FileList object and return it.
As a bit of a side note, I recently did a blog post about using your attribute type to create a composer driven photo gallery - to fetch the actual file list I used a hardcoded List Files From Set block.http://www.mesuva.com.au/blog/concrete5/creating-a-composer-driven-...
Cheers
-Ryan
I'm no real expert on attribute types, but I did build one recently where I wanted to be able to return the stored data in different formats (as HTML, as a raw array of data, etc), I'm pretty sure this is the same sort of problem you are trying to solve.
When you retrieve an attribute from a page object, the basic way to do this is:
$c->getAttribute('fileset');
This is going to call the function getValue() on your attribute, which really is supposed to return the raw data. With your attribute Seb, you haven't defined a getValue() function, it just falls back to the one in DefaultAttributeTypeController. (which works perfectly fine).
What I found out though, is that getAttribute can be passed a 'display mode' as a second argument. When you do this, concrete uses this second argument to work out the name of the function to call on the attribute.
So if you do this:
$c->getAttribute('my_attribute', 'display');
It's going to call the function getDisplayValue() on the attribute. Or if you called:
$c->getAttribute('my_attribute', 'Array');
It's going to call getArrayValue() on the attribute.
So in your case, maybe you need to call:
$fs = $c->getAttribute('fileset', 'FileListObject');
So it calls your getFileListObjectValue() function of your attribute. That function should be able to do whatever is necessary to build the FileList object and return it.
As a bit of a side note, I recently did a blog post about using your attribute type to create a composer driven photo gallery - to fetch the actual file list I used a hardcoded List Files From Set block.http://www.mesuva.com.au/blog/concrete5/creating-a-composer-driven-...
Cheers
-Ryan
Ok, you're right, this feature exist sine 5.6.2.1 and i has working on 5.6.1..
So now it works and it will help you for your blog post :-)
For the little sorry, i was searching for site that talk about C5 and click on your on google really by chance. Once n your site i've clicked on the post where you are writing about fileset_attribute. That is the starting point to improve the attribute :-)
Thanks both,
Seb
So now it works and it will help you for your blog post :-)
For the little sorry, i was searching for site that talk about C5 and click on your on google really by chance. Once n your site i've clicked on the post where you are writing about fileset_attribute. That is the starting point to improve the attribute :-)
Thanks both,
Seb