Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
kobnim CreditAttribution: kobnim commentedProblem 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:To generate the array
$field_output_array
where$field_output_array[2] = 'rendered_item2'
:Comment #2
kobnim CreditAttribution: kobnim commentedUnfortunately, 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':
Does anyone know a more robust solution, for rendering multiple-valued views fields?
Comment #3
MustangGB CreditAttribution: MustangGB commented