2

Sorry, this is a super stupid problem but I'm new to PHP and failed at solving it myself.

On my node I have an energy field (Radioactivity module) and a computed field. The computed field is supposed to read out the node's current energy, plug it into a function and return another value.

I have tried all of

float( $entity->field_energy ),

float( $entity->field_energy[0]['value'] ), and

float( array_pop(array_pop(field_get_items($entity_type, $entity, 'field_energy'))) )

to read out the node's current energy, but everythings gives me an error. When I replace the above with a static number everything works fine.

Can anyone tell me how to read out the energy? I don't even know what data type is used to store the energy.

(I couldn't add a radioactivity tag. Unfortunately I don't have enough reputation points for doing so.)

3

My guess is that it is something like

float($entity->field_energy['und'][0]['value']);

Also, you can do

print_r ($entity->field_energy);

to see what is contained in object.

also, it may be contained in node object so maybe it is

float($node->field_energy['und'][0]['value']);

My blind guess but it may help you :)

  • 2
    Just to avoid future issues with translations or new languages setup use it like this: float($node->field_energy[$node->language][0]['value']); – Emil Orol Oct 1 '12 at 13:30
  • redhatlab, excellent remark, i read from default value for simplicity (no language iterations), but your approach is better. Though, one could search through documents and replace all occurances of ['und'] when switching to multilanguage :D – NenadP Oct 1 '12 at 13:58
  • In addition, you can download/enable the [Devel Module] (drupal.org/project/devel) and call kpr() on your $node or $entity object. This is functionally the same as calling print_r, but provides a nicer output style. – joedougherty Oct 1 '12 at 15:30
  • 1
    Thanks for your help guys! This worked perfectly. I used print_r to display the field_energy array and noticed that the values are not stored with integer keys but rather with text keys. It turned out that I have to use $entity->field_energy['und'][0]['radioactivity_energy']. For some reason applying float() to this gives me an error though. But if I don't apply float() I can still use the value as a real number. – deinqwertz Oct 1 '12 at 17:58
  • try: floatval($entity->field_energy['und'][0]['radioactivity_energy']) – Emil Orol Oct 2 '12 at 2:29

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.