Adding file select attribute though a file importer?

Permalink
Hi i'm trying to add file attribute when I process importing a file through a custom file importer, however the date/time and text attributes work but not the select one?

Looking around in the forums it seems that people have had an issue with this since 5.6, however I can't find an answer.. can anyone please shed some light?

Thanks in advance...

Neither of these work...??
$file->setAttribute('series', $series);
$file->setAttribute('series', array($series));

BHWW
 
hutman replied on at Permalink Reply
hutman
Does the value of $series already exist as a value for your select attribute?
BHWW replied on at Permalink Reply
BHWW
Hi Hutman

Yes it does, as a simple string. I tried replacing it with just a ‘test’ string but still no joy?

Thanks, any idea?
mnakalay replied on at Permalink Reply
mnakalay
try this
$value = \Concrete\Attribute\Select\Option::getByValue($series);
if (is_object($value)) {
    $file->setAttribute('series', $value);
}

This code works for already existing options only, it won't add an option to the attribute itself if it doesn't already exist.

What you were doing should be working even with just the string but to work the value $series must be an existing option. Are you sure you didn't misspell the string or change the case or anything like that?
BHWW replied on at Permalink Reply
BHWW
Hi

Yes you are correct it does add it if already and option, but I need to be able to add new options too. The checkbox to allow user submissions is ticked. As I say this seems to have been an issue since 5.6, really surprised if it's not got a fix.

Thanks for the help!
BHWW replied on at Permalink Reply
BHWW
Hi, that is great it got me on the right track, this is what I ended up with for anyone looking:

use \Concrete\Core\Attribute\Key\FileKey as FileKey;
use \Concrete\Attribute\Select\Option as SelectAttributeOption;
         if ($series) { 
        $atSeries = FileKey::getByHandle('series'); 
         $atSeriesAdd = SelectAttributeOption::add($atSeries, $series);
            $file->setAttribute('series', array($series));
         }
         if ($author) { 
        $atAuthor = FileKey::getByHandle('author'); 
         $atAuthorAdd = SelectAttributeOption::add($atAuthor, $author);
            $file->setAttribute('author', array($author));
         }