Getting the value from a multi-select list PHP in Drupal site

22 Views Asked by At

I am trying to get values from a multi-select field. I think I'm getting the keys and not the values. The output is 'cell_phone' instead of 'cell phone', for example. I also am seeing a warning about 'undefined array key', category, which I thought I initialized. Here is my code. What is wrong with the code?
Thank you.

 $categorylist[] = “”;     
    foreach ($node->get('field_category') as $category_value) {
      if ( !empty($category_value ) ){
        $variables['categorylist'] .= $category_value->value . ", ";
     }
 }
 in Twig file:
 {% if categorylist %}
   {{categorylist}}
 {% endif %} 
 
0

There are 0 best solutions below