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

How can I do this?

Thanks very much!

Comments

kobnim’s picture

Status: Active » Closed (fixed)

Problem solved.

If the field is multi-valued, then $view->render_field($fieldname, $view->row_index) returns a comma-separated string of the individual rendered items:

rendered_item0,rendered_item1,rendered_item2,...

To generate the array $field_output_array where $field_output_array[2] = 'rendered_item2':

$field_output_string = $view->render_field($fieldname, $view->row_index);
$separator = ',';
$field_output_array = explode($separator, $field_output_string);
kobnim’s picture

Status: Closed (fixed) » Active

Unfortunately, the above approach is not robust. It does not work for multi-valued fields that contain commas within the field values. For example, if I have a text field that contains 20 commas, and I use the ',' separator to explode $field_output_string, then I end up exploding that text field into 21 pieces.

I tried the following as well, but this is also not a robust approach because (a) it does not support localization and (b) not all fields have 'safe_values':

$vals = $view->field[$fieldname]->get_value($row);
foreach ($vals as $i=>$v){
  $field_values[$i] = $v['safe_value'];
}

Does anyone know a more robust solution, for rendering multiple-valued views fields?

MustangGB’s picture

Issue summary: View changes
Status: Active » Closed (outdated)