0

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?

1

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, explode the string:

$field_output_string = $view->render_field($fieldname, $view->row_index);
$separator = ',';
$field_output_array = explode($separator, $field_output_string);
| improve this answer | |
  • Unfortunately, this is not a robust solution. 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. Does anyone know a more robust solution, for programmatically rendering multiple-valued views-fields? – user606696 Jul 22 '13 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.