SOLVED Jumping into attributes

Permalink
Hi,

I want to begin getting into some more advanced development with C5 and thought I'd give working with attributes a go.

I've had a look at the page on attributes and my understanding is that you can create attributes for files/pages/users and then influence what happens on the display end for that file/page/user. For example, the exclude_from_nav attribute for pages lets me determine whether a page will appear in my autonavs.

Eventually, I'm going to try and build my first block that will (hopefully) use attributes as a way to determine what content is displayed (such as 'if show_homepage attribute for a page is checked, show my block's content on the homepage').

So, with that in mind, if I created an attribute called 'image_description' in the File Manager area of C5, could I modify the view.php file of the slideshow block to display an image description below each photo?

It would be very powerful to be able to use attributes to extend certain add-ons if possible as I find myself often wanting a couple of extra bits of info here and there.

Thanks for any responses

osu

osu
 
Tony replied on at Permalink Reply
Tony
yeah, the attribute system is really powerful once you start digging into it.

you don't need to make a description for files though, because there's already a property for that: $fv->getDescription();

but if you wanted to get an attribute from a file, you'd do something like:
$fv->getAttribute('photo_film_format');

and notice that this is being done on the FileVersion object, and not the file object too. best of luck.
everydaycoder replied on at Permalink Reply
everydaycoder
My favorite way to use attributes is for allowing my client, the site owner and maintainer, to change the background and accent images on different pages. I basically define an attribute that is a Image/File type and he can change it whenever he wants. The code looks something like this:
$bgImage = $c->getCollectionAttributeValue('bgImage');
        if (!empty($bgImage)) {
                echo('<img src="'.$bgImage->getRelativePath().'"/>');
        }
osu replied on at Permalink Reply
osu
Thanks for both your replies, that's useful info for me to begin experimenting with.

alyawn - how did you create an Image/File attribute type? That method sounds like it might have the potential to allow a client to 'attach' a file to a specific user in the admin end that could then be displayed as a download link on that user's profile page.

That's something I wanted to do in a project a while ago but had no idea how to. Have you done that before?
everydaycoder replied on at Permalink Reply
everydaycoder
osu, the Image/File attribute type is built into concrete5 already. By default, it's only associated with Collections (which includes Pages). To enable it for users, you need to create the association. Goto Dashboard -> Sitewide Settings ->Attributes (tab). Check the box for Image/File under User. Now, you can add a User Attribute of type Image/File for users. HTH.
osu replied on at Permalink Reply
osu
Hi Tony,

Have been trying out various ways to echo out a files attribute in my footer.php file, but failing miserably.

I'm doing this:

<?php
$img_adesc = $fv->getAttribute('img_meta');
echo $img_adesc;
?>


Which gives me an error (Call to a member function getAttribute() on a non-object). And have tried a few other methods like this

<?php
$img_adesc = $c->getAttributeValue('img_meta');
echo $img_adesc;
?>


But that displayed nothing.

Do I need to call a Helper class first?

Thanks
Tony replied on at Permalink Best Answer Reply
Tony
are you trying to get the collection attribute of a FileVersion or a Collection/Page?

I'm assuming you're working with file attributes.

first, load the file. there are a few ways to do this, but here I'm just loading it by file ID (3):

$fileObject = File::getByID(3);

and then you want to get the most recent file version from that file object, like this:

$fv = $fileObject->getApprovedVersion();

then you can get the attribute:

echo $fv->getAttribute('my_attribute_handle');

with collections, it's even easier to get the attribute. all you have to do is to load the collection, either by path:

$aboutPage = Page::getByPath('/about');

or by collection id:

$somePage = Page::getByID(5);

and then print the attribute:

echo $somePage->getAttribute('my_attribute_handle');
andrew replied on at Permalink Reply
andrew
Addendum to this: you actually don't need to run $f->getApprovedVersion() either. If you don't, the approved version will be automatically loaded and operated on. So this:

$f = File::getByID(4);
print $f->getAttribute('img_meta');


Should work just fine, and grab the "img_meta" attribute from the approved version of file #4.
osu replied on at Permalink Reply
osu
Thanks to both of you for explaining that.

I'm trying to use a real-life example of how I can use this - I've copied over the image block from /concrete/ to /blocks/ and am modifying the view.php file. I want to print out the alt text and my new attribute img_meta below the image I choose.

* EDIT *

Found a solution for how to get the current files attribute value. I looked in the controller file for the image block and am using the functions within that class to identify the current files ID. Then I can use that like Tony and Andrew have said above to print out the attributes value.

I've commented my code to make it as easy as possible for anyone else wanting to do the same to see what's happening. Don't forget that this solution relies on the functions in the image block controller as I am just modding the image block to do what I want:

<?php 
defined('C5_EXECUTE') or die(_("Access Denied."));
// Put image alt text and img_meta attributes in variables
   $am_fileID = $controller->getFileID(); // Get current file ID
   $f = File::getByID($am_fileID); // Use the files ID to get the file object
   $img_meta = $f->getAttribute('img_meta'); // Put the img_meta attribute in a variable
   $am_alt_text = $controller->getAltText(); // Same with the alt text
// Use alt text and img_meta attribute to fill descriptive spans on image blocks:
   echo '<div class="caption">';
   echo($controller->getContentAndGenerate());
   echo '<span class="title">' . $am_alt_text . '</span><span class="anotherdesc">' . $img_meta . '</span></div>';
?>


Thanks for all your help guys, first step (of many I'm sure).
Tony replied on at Permalink Reply
Tony
oh cool, didn't realize that you could do that now. thx.