Programmatically add option list attribute options

Permalink
I have an "option list" page attribute that needs a lot of options.

Rather than insert them all manually, I'm working on importing the options from a CSV file. However, I'm not sure how to insert and save them.

I've done the below, assuming that feeding in an array of strings would do, but with no luck.

$ak = CollectionAttributeKey::getByHandle('attribute_handle');
$ak->getController()->setOptions([
   'Option 1',
   'Option 2',
   'Option 3'
]);

 
mnakalay replied on at Permalink Reply
mnakalay
which version of C5 are you using?
shahroq replied on at Permalink Reply
shahroq
//use Concrete\Core\Attribute\Type as AttributeType;
//use Concrete\Attribute\Select\Option as SelectAttributeTypeOption;
//first check if attribute with current handle exists:
$key = CollectionAttributeKey::getByHandle('attribute_x');
if (!is_object($key)){
    $attr_type = AttributeType::getByHandle('select'); 
    $desc = array ( 'akHandle' => 'attribute_x',
                    'akName'=> t('Attribute X'),
                    //'asID' => $attributeSetID
                  );
    $key = CollectionAttributeKey::add( $attr_type, $desc );
    $key_option = SelectAttributeTypeOption::add( $key, 'key 1');
    $key_option = SelectAttributeTypeOption::add( $key, 'key 2');
    $key_option = SelectAttributeTypeOption::add( $key, 'key 3');
}
mnakalay replied on at Permalink Reply
mnakalay
@shahroq are you sure this still works in v8?

A different way of doing it that uses modern c5 can be found here:https://www.concrete5.org/community/forums/customizing_c5/selectattr...

It is definitely more complex and that example is to be used in a package but easy to adapt
shahroq replied on at Permalink Reply
shahroq
@mnakalay: Yes, it works in both 5.7 and 5.8.