Save select attribute options

Permalink
I cannot figure out how to save select attribute options in 5.7. I have created a page select attribute and I get the option list in the following way:

use Concrete\Core\Attribute;
use Concrete\Core\Attribute\Type as AttributeType;
use Concrete\Core\Attribute\Key\CollectionKey;
use Concrete\Attribute\Select\Controller as SelectController;
 ...
$ak = CollectionKey::getByHandle('my_select_attribute');
$at = AttributeType::getByHandle('select');
$satc = new SelectController($at);
$satc->setAttributeKey($ak);   
$values = $satc->getOptions()->getOptions();
$options = array();
foreach ($values as $key => $value) {
   $options[$key] = $value->getSelectAttributeOptionValue();
}
$this->set('my_options', $options);


Now in my form, I have the following select input:

$form->selectMultiple('my_select_attribute', $my_options);


I cannot figure out, how to save the selected options in my controller. In 5.6 I did it the following way:
$select = SelectAttributeTypeOption::getByID($this->post('my_select_attribute'));
$page->setAttribute('my_select_attribute', $my_select_attribute);


However, if I try to import the namespace Concrete\Attribute\Select\Option I get an error, so I cannot use the Method above.

Any help is much appreciated.

creatingweb
View Replies:
andrew replied on at Permalink Reply
andrew
Looking at that thread there are a couple things missiong.

1. The initial options array in the page should have its array set to the option object ID, not just the numerical key. So instead of this:

foreach ($values as $key => $value) {
$options[$key] = $value->getSelectAttributeOptionValue();
}

Do this:

foreach ($values as $key => $value) {
$options[$value->getSelectAttributeOptionID()] = $value->getSelectAttributeOptionValue();
}

2. Next, here's how you should build the select options array in the submit code:

$selectedOptions = array();
foreach($this->post('my_select_attribute') as $optionID) {
$option = \Concrete\Attribute\Select\Option::getByID($optionID);
if (is_object($option)){
$selectedOptions[] = $option;
}
}

This will get you an array of option objects. Then you can pass those to setAttribute()

$c->setAttribute('my_select_attribute', $selectedOptions);
andrew replied on at Permalink Reply
andrew
Update: this may not work with 5.7.3.1 and earlier, due to a bug in the attribute controller. We've fixed this in Github and it'll be released in 5.7.4