I have a custom row template, and I know I can obtain the rendered field output with:
$field_output = $view->render_field($fieldname, $view->row_index);
For a certain multi-valued field, I would now like to create an array of the rendered output of each item in the field.
$field_output[3] = rendered field output for 3rd item in my multi-valued field
Anyone know how I can do this?
UPDATE:
There are two approaches I am aware of, but neither is a good, robust solution.
Approach #1: get_values()
One approach is to call get_values, and then grab the safe_value:
$vals = $view->field[$fieldname]->get_value($row);
$field_values = array();
foreach ($vals as $i=>$v){
$field_values[$i] = $v['safe_value'];
}
The problem with this approach, as described here, is that (a) it breaks localization and (b) some fields do not have a safe_value.
Approach #2: render_field()
The second approach (see below) uses render_field() and then explodes the result:
$field_values = array();
$field_output_str = $view->render_field($fieldname, $view->row_index);
if (!empty($field_output_str)){
$separator = ',';
$field_values = explode($separator, $field_output_str);
}
The problem with this approach is that, in addition to exploding multi-valued fields, it also explodes individual fields that contain commas, such as text fields.
Does anyone have a better, more robust solution?