2

I have a regular field with multiple values, If i do

render( $fields['field1'] )

It prints

Field1_value1
Field1_value2
Field1_value3
etc

Am trying to get it to output like this

[Field1_value1]
[Field1_value2]
[Field1_value3]
etc

To add [ and ] to the output. I thought of using a foreach loop, but the field array is like such

'#title' => 'xxxx',
'#type' => 'xxxx',
...
'0' => <array>,
'1' => <array>,
'2' => <array>,

How can I get it to loop throught the [0], [1], [2] etc only?

Note: I have no control on how many multiple values the field may have, so I have to loop.


Another solution that worked well for me: Display comma-separated list of taxonomy terms?

You can make use of the power of field.tpl.php.

1
$field_values = element_children($fields['field1']);
foreach($field_values as $field_value) { ... }

I was looking for the answer to this too, and ended up hunting through the render() function. It wasn't too far down the chain: drupal_render() calls element_children().

element_children

Identifies the children of an element array, optionally sorted by weight.

The children of a element array are those key/value pairs whose key does not start with a '#'. See drupal_render() for details.

| improve this answer | |
-1

Why don't you try a regular expression loop after the print? See here.

If you're really experiencing problems with the loop, check out the documentation for preg_replace_callback().

| improve this answer | |

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.