0

I have a field which accepts unlimited values so the values of the field get saved in an array. If I use token_get_values() to get the corresponding field value, I only get the first element of the array.

How can I read the whole array value or get an imploded string of values with token_get_values()?

1

You cannot, using token_get_values(). The code that is returning the value of the token for a CCK field uses just the first value of the field. The function that gets the value of a token for a CCK field (content_token_values()) contains the following code. (See in particular the code between // [1].)

  $content_type = content_types($node->type);
  $node->build_mode = 'token';
  $node->content = array();
  content_view($node);
  // The formatted values will only be known after the content has been rendered.
  drupal_render($node->content);
  content_alter($node);

  $field_types = _content_field_types();
  foreach ($content_type['fields'] as $field_name => $field) {
    $items = isset($node->{$field_name}) ? $node->{$field_name} : array();
    $function = $field_types[$field['type']]['module'] . '_token_values';
    if (!empty($items) && function_exists($function)) {
      // [1]
      $token_values = (array) $function('field', $items, $options);
      // [1]
      foreach ($token_values as $token => $value) {
        $tokens[$field_name .'-'. $token] = $value;
      }
    }
  }

The function called in $function('field', $items, $options) is, for a text field, text_token_values(), which contains the following code.

  if ($type == 'field') {
    $item = $object[0];

    $tokens['raw']  = $item['value'];
    $tokens['formatted'] = isset($item['view']) ? $item['view'] : '';
    return $tokens;
  }

Despite content_token_values() passing all the CCK field values, the function just uses the first value.

| 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.