**SOLVED** Get ANY File Attribute from Page List

Permalink 1 user found helpful
Hi!

I just created a Page List template that allows me to upload and link to files using attributes, however, I need to display the file name too...

I've tried the attributes found in the C5 cheat sheet, but maybe I'm doing it wrong. This is what I tried:
$song = $fv->getTitle();

Then I called in the variable, like so
<?php if($song1){echo("<li data-mp3=\"$song1\">$song</li>");}?>


Noticed the '$song'? It doesn't display anything...

Any help would be greatly appreciated. Thanks in advance.

NBardales
 
marcsublet replied on at Permalink Reply
marcsublet
http://www.concrete5.org/community/forums/customizing_c5/getting-the-attributes-of-a-file/

Loader::model('attribute/categories/file');
if(is_object($fobj)){
//using $fobj as the object
//$fobj->fID = The id of the file in question
//$fobj->fvID = The version of the file to use
$attribs = FileAttributeKey::getAttributes($fobj->fID,$fobj->fvID);
//$attribs returns a private object so in order to access you need to use
$attribute_value = $attribs->getAttribute('$attribute');
//where '$attribute' is your attribute handle
}
$fobj->getTitle();
$fobj->getDescription();


But in your case, why your variable is called $song and you try to get title from $song1 ?
NBardales replied on at Permalink Reply
NBardales
Sorry, I think I didn't add enough information...
I'm adding the file using a page attribute ($song1)...

So I just came to my own conclusion that getting the title of an attribute file might not be that simple (and that's why I should never post that late)...

Thanks for your answer, I still need to get some file titles, so that should do the trick.
jordanlev replied on at Permalink Reply
jordanlev
Once you have a file object (I'm not sure how you're getting this -- via File::getByID(), or from a collection attribute while looping through the page list, or something else), you can get the file's title like this:
$file->getTitle();

And you can get custom file attributes like this:
$file->getAttribute('your_file_attribute_handle');


Again, I'm not sure what's going on in your code and what you did before the two snippets you posted, but I can see that first of all, you're doing $fv->getTitle(), which indicates you've gotten a file version -- this is not necessary because you can get attributes on the file itself and it will automatically pull from the latest file version for you. Also, you're saying that $song1 isn't outputting anything, but that's because you're putting the title into the "$song" variable, not "$song1" (no number 1 at the end).

Hope that helps.
NBardales replied on at Permalink Reply
NBardales
Thanks for posting jordanlev! You always give some amazing answers...

Let me see if I can explain it better...
I created a discography theme and (to make it easier for the user) the albums are added using Pages, i.e. I created a Page List template that pulls pages with the type 'Album'. When editing the Page Type 'Album' in Composer, you add files using Page Attributes, then, those attributes are displayed in the Home Page (which includes a Page List with the 'Album' template. Each 'attribute file' represents a song, which means a single album-type page can have up to 12 'attribute' files (if needed).

This is my Page List Custom Template code:
<?php 
   defined('C5_EXECUTE') or die(_("Access Denied."));
   $textHelper = Loader::helper("text");
   // now that we're in the specialized content file for this block type, 
   // we'll include this block type's class, and pass the block to it, and get
   // the content
   if (count($cArray) > 0) { ?>
   <?php 
   for ($i = 0; $i < count($cArray); $i++ ) {
      $cobj = $cArray[$i]; 
      $title = $cobj->getCollectionName();
      $cover = $cobj->getCollectionAttributeValue('album_cover')->getRelativePath();
      $song1 = $cobj->getCollectionAttributeValue('song1')->getRelativePath();
      $song2 = $cobj->getCollectionAttributeValue('song2')->getRelativePath();
      ?>


What I don't know is HOW to get the title of a file that is an attribute. (that was confusing...)

'$song 1' is an 'Image/File' Page Attribute, I need to get its name! (that's better)
jordanlev replied on at Permalink Best Answer Reply
jordanlev
I understand.

Your code is ONLY grabbing the path of each song file, and isn't doing anything else, so you only have the file path available (because that's all you told it to do). What you need to do instead is have your code grab all of the info you need, not just the file path. So change this:
$cover = $cobj->getCollectionAttributeValue('album_cover')->getRelativePath();
$song1 = $cobj->getCollectionAttributeValue('song1')->getRelativePath();
$song2 = $cobj->getCollectionAttributeValue('song2')->getRelativePath();

...to this:
$cover = $cobj->getAttribute('album_cover');
if ($cover) {
    $cover_title = $cover->getTitle();
    $cover_path = $cover->getRelativePath();
}
$song1 = $cobj->getAttribute('song1');
if ($song1) {
    $song1_title = $song1->getTitle();
    $song1_path = $song1->getRelativePath();
}
$song2 = $cobj->getAttribute('song2');
if ($song2) {
    $song2_title = $song2->getTitle();
    $song2_path = $song2->getRelativePath();
}


Now you have different variables for each different piece of information about each song. So further down in your template, change the code that's like this:
<?php if($song1){echo("<li data-mp3=\"$song1\">$song</li>");}?>

...to something like this:
<?php if($song1){echo("<li data-mp3=\"$song1_path\">$song1_title</li>");}?>
NBardales replied on at Permalink Reply
NBardales
Thank you very much jordanlev... Right as always! That worked like a charm!