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.
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' ]);
which version of C5 are you using?
//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'); }
@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
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
@mnakalay: Yes, it works in both 5.7 and 5.8.