1

I'm using field_info_instances function to retrieve all fields info under a content type. (ex: field_info_instances('node','bundle_name') ).

But the return array fields are not ordered as I expected. I need to get fields in order as I mentioned in content type (as per weights).

For ex, a filed weight is 8 (8th position from top) in manage fields list. but in the return array it is displaying in top.

Is there any possibility to get the fields in order?

Manage fields screen (below) enter image description here

field_info_instances output (below)

enter image description here

2

You probably want something like this

$info = field_info_instances('node','bundle_name');

uasort($info, function($a, $b) {
  $a_weight = (is_array($a) && isset($a['widget']['weight'])) ? $a['widget']['weight'] : 0;
  $b_weight = (is_array($b) && isset($b['widget']['weight'])) ? $b['widget']['weight'] : 0;

  if ($a_weight == $b_weight) {
    return 0;
  }

  return ($a_weight < $b_weight) ? -1 : 1;
});

// $info is now sorted by weight.

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.