Multi-select attribute value display as comma separated

Permalink
Hello all
Currently I'm displaying an Multi-select attribute value in page list blocks view by the below code:
<?php echo $page->getAttribute('countries_visited');?>

However its displaying the output without any comma like:
United Kingdom Canada India South Africa


Its hard to read from this. Also I cannot use str_replace function to replace space with comma because there are few countries with spaces itself. So what is the best solution to display them comma separated?

My C5 version is 8.0.3

ronyDdeveloper
 
c5dragon replied on at Permalink Reply
c5dragon
In an older 5.7.5.13 project I did this:
$project_type = $c->getAttribute('project_type');
    if (count($project_type) && is_array($project_type)) {
        usort($project_type, "orderNode");
        $i=0;
        foreach($project_type as $topic) {
            if ($i>0) { $project_typeDisplay .= ', ';}
            $project_typeDisplay .= $topic->getTreeNodeDisplayName();
            $i++;
        }
    }

<?php echo $project_typeDisplay ?>
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Thanks for your response. Its not working here because
$project_type = $c->getAttribute('project_type');
this returns string, not array.

Thanks
c5dragon replied on at Permalink Reply
c5dragon
Not an object?

With the attribute display block you can output a separated list:
And maybe you can use this code in your block.
if (!empty($this->delimiter)) {
            $parts = explode("\n", $content);
            if (count($parts) > 1) {
                switch ($this->delimiter) {
                    case 'comma':
                        $delimiter = ',';
                        break;
                    case 'commaSpace':
                        $delimiter = ', ';
                        break;
                    case 'pipe':
                        $delimiter = '|';
                        break;
                    case 'dash':
                        $delimiter = '-';
onchones replied on at Permalink Reply
So I had a similar issue that may or may not apply.

I was trying to grab a user's attribute, but the attribute was a multiple select option list. I wanted to grab all of the selected values for that attribute. I did this:

$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$user_courses = h($ui->getAttribute('user_favorite_colors'));


And I would get back the values like this:

orange black green red


I tried to do a php explode to force it into an array, but it didn't work, because I didn't know what the delimiter was. At first I thought it was spaces, but it wasn't! There was actually no spaces separating the values. It was some sort of line break, like this:

orange
black
green
red


And besides that, it's not great to use spaces to separate the values because, depending on the attribute, sometimes values will have spaces within themselves.

In the end, since I figured out that the delimiter was a line break of some sort, I did this:

$favorite_color_array = preg_split('/\r\n|\r|\n/', $user_favorite_colors);


Basically detecting different possible line breaks and using that to separate the values. I never bothered to figure out which kind of line break it really was. In any case, this was successful in separating the values so it was a proper array called $favorite_color_array.

All in all it looks like this:

$u = new User();
$ui = UserInfo::getByID($u->getUserID());
$user_courses = h($ui->getAttribute('user_favorite_colors'));
$favorite_color_array = preg_split('/\r\n|\r|\n/', $user_favorite_colors);
print_r($favorite_color_array);


I hope this helps someone else!
mnakalay replied on at Permalink Reply
mnakalay
I think this should give you the ist of options as an array
$selectedOptions = [];
$u = new User();
$ui = $u->getUserInfoObject();
$value = $ui->getAttributeValueObject("user_favorite_colors");
if ($value && $value->getValue()) {
    $selectedOptions = $value->getValue()->getSelectedOptions();
    $selectedOptions = $selectedOptions->toArray();
}