4

I am trying to update a node with a standard multi value text field programatically with the following:

$node = customfunctionreturnsnode();
$node->field_scores['und'][$dayof]['value'] = $total;
node_save($node);

More info: $dayof is a custom variable, that can be from 1 - 28. $total is numerical value that is determined elsewhere in the code.

Eventually the field will have 28 values, but they might not get entered in the 'correct' order. So I would like to be able to have my code above select which delta to add it to. Also, the values could get updated, so this will allow me to update the correct value.

It seems to work, and the db shows the right delta. But when I add a new value the previous delta resets to 0. The new delta has the correct value, but when I add another one the second value's delta goes to 1... and so on.

I checked, and the delta field is not an auto-increment table... but I'm not really sure where to look from here. I couldn't find anyone else that has this issue. Am I doing something wrong?

Thank you for you time.

1 Answer 1

3

I think I found a solution. I used hook_node_presave to check if a value was set, ad if not to set it to -1 for the 28 fields I have. Maybe not the most elegant (I would love to hear if there is a more drupally way to do this) but it works.

function mymodule_node_presave($node) {
if ($node->type == 'mynodetype') {
for ($i = 0; $i < 29; $i++) {
if (!isset($node->field_scores_a['und'][$i]['value'])) {
 $node->field_scores_a['und'][$i]['value'] = -1;
}
if (!isset($node->field_scores_b['und'][$i]['value'])) {
 $node->field_scores_b['und'][$i]['value'] = -1;
}
if (!isset($node->field_scores_c['und'][$i]['value'])) {
  $node->field_scores_c['und'][$i]['value'] = -1;
}
}
}
}

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.