setAttribute for a 'multiple select' checkbox

Permalink
Hi there, I'm back in the realm of fiddling with page attributes and setting them programmatically...

I know how to change a page attribute for a checkbox by using:
$c->setAttribute('attribute_handle', $variable);
where $variable is 1 or 0... but how do I do the same for a 'dropdown select' where 'multiple values' are checkboxes?

Also, while I'm here, it would be handy to know how to change a page attribute which is a normal 'dropdown select' (as I can see that one cropping up later on)

If you can give me a pointer, I'd be very grateful

Thanks

rc255
 
hutman replied on at Permalink Reply
hutman
Here is some code we have used with 5.6 to do that

//make an array of the values that you want to set
$pageTags = array('tag', 'tag1', 'tag2');
$ak = CollectionAttributeKey::getByHandle('tags');
$tagValues = array();
foreach ($pageTags as $tag) {
        //loop over each value and check if that value already exists, if not create it
        if (!$tagValueObject = SelectAttributeTypeOption::getByValue($tag, $ak)) {
                $tagValueObject = SelectAttributeTypeOption::add($ak, $tag);
        }
        $tagValues[] = $tagValueObject;
}
//set the attribute to the array of validated values
$page->setAttribute('tags', $tagValues);


For single select dropdowns you can set it just like the other attribute, but you need to make sure that the value you are setting exactly matches one in the select list (case sensitive).
rc255 replied on at Permalink Reply
rc255
Thanks dude, I didn't want to hassle you again after helping with the last one... Once I get these last bits working, I'll drop you a link to show you it all working... I'm rather pleased with it all so far :)

Although, I might be back later to ask another question about this if you don't mind...

Thanks again

Rob
hutman replied on at Permalink Reply
hutman
Ask away, that's what the community is for :)