setAttribute thumbnail programmatically

Permalink
Hi, I'm trying to set a thumbnail attribute programmatically. I have the attribute setup for the page - 'thumbnail' and I have the image uploaded into the filemanager and I know the ID of the image (in this case '235')...

I'm thinking that it's something along the line of:
$newpage->setAttribute('thumbnail', $file->getFile()->getFileID());

but the finer points are eluding me...

Am I on the right track?

rc255
 
hutman replied on at Permalink Reply
hutman
Depending upon what your $file variable is you are on the right track, you want to pass the File ID into the setAttribute function
rc255 replied on at Permalink Reply
rc255
That's the bit that's eluding me.... :)

What do I need to do to setup the $file variable?

$file = **all I know is that the file is called logo.jpg and that it is in the filemanager with ID 235**
$newpage->setAttribute('thumbnail', $file->getFile()->getFileID(235));


Is that nearly right? :)
hutman replied on at Permalink Reply
hutman
If you just want to hard code that one id all you have to do is

$newpage->setAttribute('thumbnail', 235);
rc255 replied on at Permalink Reply
rc255
Brilliant! Once again, even simpler than I thought... I like to try and make things more difficult for myself!

Thanks for your help again mate!

Rob
rc255 replied on at Permalink Reply
rc255
Hrm, I got excited too soon... it doesn't seem to be doing it.

Is that all the info I need to put in? 'handle' & 'id'?

I can't think what else it would want from me...
hutman replied on at Permalink Reply
hutman
Yes, that is all the information you need. Are you sure that your $newpage variable is the correct page object?
rc255 replied on at Permalink Reply
rc255
Yeah, as I've just tagged that line onto the bottom of a whole load of other attributes that are set when the page is created... very strange

all the other attributes are being set
rc255 replied on at Permalink Reply
rc255
It wouldn't be anything to do with filemanager permissions would it? These pages are being created by a user (logged in but not accessing any dashboard stuff)

...although that wouldn't make much difference as they are able to create the page and add all the other attributes
hutman replied on at Permalink Reply
hutman
This shouldn't have anything to do with permissions, your "thumbnail" attribute is an image type attribute, right?
rc255 replied on at Permalink Reply
rc255
Ahahaha, I have sussed it out... finally...

I have to create a fileObject first... so instead of
$newpage->setAttribute('thumbnail', 235);

I give it:
$fObj = File::getByID(235); // setup file object based on fileID then:
$newpage->setAttribute('thumbnail', $fObj); // then set the fileObject as the $tring


This then successfully sets the page image thumbnail
Yes!