Express - Programmatically Add Select, and set Options List

Hey so I’m trying to figure out the correct steps for setting the Options List when adding an Attribute of type Select to an Express Object I’m building Programmatically… and falling on my face trying to figure it out… Can anyone help me please? This is what I have so far

        $object = Express::buildObject('test1', 'test1', 'Test1');
        $object->addAttribute('user_selector', 'Owner User', 'test_owner');
        $settings = new \Concrete\Core\Entity\Attribute\Key\Settings\SelectSettings();
        $settings->setAllowMultipleValues(false);
        $settings->setDisplayMultipleValuesOnSelect(false);
        $settings->setHideNoneOption(true);
        $settings->setAllowOtherValues(false);
        $list = ['test1', 'test2'];
        $optionList = new \Concrete\Core\Entity\Attribute\Value\Value\SelectValueOptionList();
        $optionList->setOptions($list);
        $settings->setOptionList($optionList);
        $object->addAttribute('select', 'Test Select', 'test_select', $settings);
        $object->save();

I’ve just wasted so many hours trying to figure out how to add/edit/remove or whatever options for this, and I really think I need help.

@BloodyIron

I think you’re using setOptions in a wrong way.

$list = ['test1', 'test2'];
$optionList = new \Concrete\Core\Entity\Attribute\Value\Value\SelectValueOptionList();
foreach ($list as $i => $value) {
    $option = new \Concrete\Core\Entity\Attribute\Value\Value\SelectValueOption();
    $option->setSelectAttributeOptionValue($value);
    $option->setOptionList($optionList);
    $option->setDisplayOrder($i);
    $optionList->getOptions()->add($option);
}
$settings->setOptionList($optionList);
1 Like

WOOO THIS WORKED TOO! And yeah, I know I was using it in a wrong way, but I couldn’t tell how I should use it, hah! Thanks again! :smiley: