1

Somehow my custom fields ('datum' and 'high') are not set in the following script. Could someone tell me what I am doing wrong?

    function erstelle_kursenodes() {

$content = file_get_contents('http://www.xmlcharts.com/cache/precious-metals.php?format=json');

if(empty($content)){
    echo 'KEINE API-VERBINDUNG';
} else {
$cuobj = json_decode($content);
  // add node properties
  $newNode = (object) NULL;
  $newNode->type = 'kurs';
  $newNode->title = 'XAUUSD';
  $newNode->uid = 0;
  $newNode->created = strtotime("now");
  $newNode->changed = strtotime("now");
  $newNode->status = 1;
  $newNode->comment = 0;
  $newNode->promote = 0;
  $newNode->moderate = 0;
  $newNode->sticky = 0;
  // add fields
  $newNode->field_datum[$node->language][0]['value'] = date('Y-m-d h:i:s', strtotime("now"));
  $newNode->field_high[$node->language][0]['value'] = number_format($cuobj->usd->gold * 31.1034768, 2, '.', '');
  // save node
  node_save($newNode);
}}

'datum' is a regular date field

'high' is decimal integer with two digits after a point

2 Answers 2

0

It is a format issue that you are choosing to save.

    $newNode->field_datum['und'][0]['value'] = date('Y-m-d h:i:s', strtotime("now"));
//Here in place $node->language pass 'und' and then try.
//Just save a node directly from backend, let say it is saved with node id 10.

$nid = 10;
echo "<pre>";
print_r(node_load($nid)) ;
echo "</pre>";

Now check which format it is using to save. Pass same format once statically and then make it dynamic. Hopefully it will resolve your issue.

1
  • 'und' did the trick, i guess my fault was too much copypasting and getting blind of so many letters, many thanks
    – tobik999
    Aug 4, 2014 at 14:20
0

$node isn't defined anywhere in your function, so $node->language will be NULL.

You probably meant:

$newNode->field_datum[LANGUAGE_NONE][0]['value'] = date('Y-m-d h:i:s', strtotime("now"));
$newNode->field_high[LANGUAGE_NONE][0]['value'] = number_format($cuobj->usd->gold * 31.1034768, 2, '.', '');

Or to use a specific language code instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.