-2

I am trying to get fields value in php but I cannot make it work. My code

<?php $value = $node->field_price; return $value; ?> 

Can somebody please help me to solve this? I believe it has to be something very simple.

1
  • Welcome to Drupal Answers! Please make your questions more detailed. Without any context, it is not possible to understand what is not working; it could be your are trying to access $node where it is not available, or it could be the node doesn't contain the field you are trying to access. When talking about code, it is very important to show which function contains the code and, in the case it is a custom hook used by a module, to say that. Commented Apr 14, 2014 at 12:17

3 Answers 3

1

You should first load your node with $node = node_load($nid). You can also use the Devel module (dpm(noad_load($nid))) to debug the node and make sure you're calling the right path to the value.

0
1

The values of fields are stored a bit deeper in $node->field_price, to be more specific they're stored like this:

$node->FIELD_NAME['LANGUAGE_CODE'][INDEX]['value'] 

The LANG_CODE is usually 'und' or undefined (if you run a multi-lang setup they correspond to the fields lang). The index specifies what exact value are you looking for (makes sense just for fields with cardinality higher than 1).

E.g. if you have a field with infinite cardinality that stores 3 taxonomy tags, you'll have three separate values under:

$node->FIELD_NAME['LANGUAGE_CODE'][0]['value'] $node->FIELD_NAME['LANGUAGE_CODE'][1]['value'] $node->FIELD_NAME['LANGUAGE_CODE'][2]['value'] 
1
  • Note that instead of 'und', use the LANGUAGE_NONE constant. Commented Dec 15, 2014 at 7:15
-1

Try this safer method:

 $node = noad_load($nid); if (isset($node->field_price[LANGUAGE_NONE][0]['value'])) { $value = $node->field_price[LANGUAGE_NONE][0]['value']; } 
1
  • Why did this get voted down? It looks like a good answer to me. Commented Dec 15, 2014 at 7:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.