Retuning page attributes as an array

Permalink
To output page attributes I'm using:

$page = Page::getCurrentPage();
echo $page->getAttribute('handle');

If the attribute type is 'Select' then multiple selections are all output separated by spaces. Is it possible to get all selected values as an array so that I can format the output?

jawbonelid
 
stromberg replied on at Permalink Reply
stromberg
I'm not an expert, but you could try something like this:

$multipleSelection = $page->getAttribute('handle');
// I assume $multipleSelection looks like this: "red green blue black"
$multipleSelectionArray = explode(" ", $multipleSelection);
echo $multipleSelectionArray[0]; // red
echo $multipleSelectionArray[1]; // green
echo $multipleSelectionArray[3]; // black
// you could of course also use a loop:
foreach ($multipleSelectionArray as $singleValue) {
    echo "Value: $singleValue<br />\n";
}


I guess there's a more elegant way - maybe one of the experts knows more :)

HTH!
jawbonelid replied on at Permalink Reply
jawbonelid
That works, until the values have spaces in them :)

I'm sure there must be functions to access this but I don't know where to start looking.

Joe
Mnkras replied on at Permalink Reply
Mnkras
$selectedOptions = $c->getAttribute('handle');
if($selectedOptions instanceof SelectAttributeTypeOptionList && $selectedOptions->count() > 0) {
foreach($selectedOptions as $opt) {
echo $opt
} 
}

simple as that
jawbonelid replied on at Permalink Reply
jawbonelid
Muchas gracias. How simple was that!
I don't understand how echo $c->getAttribute('handle') works if it's an array though, normally the output would be 'Array'?
Mnkras replied on at Permalink Reply
Mnkras
do a var_dump() on it, its an object that has an array
stromberg replied on at Permalink Reply
stromberg
Ouch, sorry :(

Have you tried browsing throughhttp://www.concrete5.org/api/ ?

(If you do find something, please post it!)

Thanks + good luck!