6

Hi,

I'm new to Craft and VERY new to plugin development. I'd like to display a thumbnail image in my table list view and thought it would be possible to accomplish using a new Custom Element Type. Does anybody know how to achieve this?

I have managed to output custom fields in the table using defineTableAttributes() but I can't output any related Assets - in particular their filenames. Do I need to define a relation in the ElementCriteriaModel using the defineCriteriaAttributes() function? Can anyone explain how this is done?

Many thanks

6

Ok, so I've managed to get an answer to this courtesy of Brandon Kelly. It's much more simple than I thought. Firstly, you do not have to specify a relatedTo parameter the defineCriteriaAttributes(). Craft takes care of this automatically. So in order to retrieve the details of an asset in a table list view you do the following in the getTableAttributeHtml() function in your custom Element Type class:

$relatedAsset = $element->myAssetsField->first();

This simple line of code gets a related myAssetsField of your element. I'm not sure why, but this object is placed in an array... Maybe someone can tell me why this is the case?

I then took the fist element of the array and used a the getThumbUrl() function which is really handy for quickly outputting the url to a thumbnail. I passed a size of 60 pixels.

case 'myAttribute>':
{   
   if(count($relatedAsset) > 0) {
      return '<img src="'.$relatedAsset[0]->getThumbUrl(60).'">';
   }
   return '';
}

The result is a thumbnail in your table list! Hope this helps others....

| improve this answer | |
  • Nice to see this is that simple. Thanks for sharing, brasso! – carlcs Jan 22 '15 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.