Programmatically set select values

Permalink
I am building a page importer and I have an "Option List" page attribute that allows for multiple values to be selected, and I am attempting to programmatically generate a page with the correct options selected. The importer provides a list of values that need to all be selected, so I am trying to figure out how to select multiple option based their value for the page attribute.

I am using Concrete5.8.3.2.

use Core;
use Concrete\Core\Attribute\Key\CollectionKey as CollectionAttributeKey;
use Concrete\Core\Entity\Attribute\Value\Value\SelectValue;
use Concrete\Core\Entity\Attribute\Value\Value\SelectValueOption as SelectAttributeTypeOption;
// Get the select list - Options are 'Category One', 'Category Two', 'Category Three', 'Category Four', and 'Category Five'
$cak = CollectionAttributeKey::getByHandle('categories');
// Sample array of options that I want to select for this page
$optionsArray = array(
   'Category One',
   'Category Two',
   'Category Four'
);
// Create an array to hold the option value objects
$optionValueObjArray = array();
// Loop through all the desired options in the array


I believe that the syntax is wrong inside the loop where I am checking that the option value exists, but I am not sure what the correct code would be. Any help would be appreciated. Thanks!

ScottSandbakken
 
ScottSandbakken replied on at Permalink Reply
ScottSandbakken
I was able to get this to do what I need. It was actually quite simple.

Here is the code in case anyone else can use it.

// Get the page to update
$page = Page::getByID(123);
// Sample array of options that I want to select for this page - These valuse must match the select value exactly!
$optionsArray = array(
   'Category One',
   'Category Two',
   'Category Four'
);
// Set the page attribute to match the array of validated values
$page->setAttribute('categories', $optionsArray);