I was writing an application that took advantage of the matrix field and (for other reasons) had to call $node = node_load($nid) on a node with a matrix field and then it later called node_save($node) on that same node. When this happened, the contents of the matrix field were removed. This is what I was doing to see the problem:

$node = node_load($nid);
dpr($node->field_matrix_field_field);

node_save($node);
$node = node_load($nid, NULL, TRUE); // to not load the node from cache
dpr($node->field_matrix_field_field);

I tracked the problem down and what I think is happening is the node_load is returning an array of matrix field rows for $node->field_matrix_field_field, but node save is expecting an array called 'matrix' that contains the array of matrix field rows. I was able to do a workaround like this, but assume there is a better solution:

$node = node_load($nid);
$node->field_matrix_field = array('matrix' => $node->field_matrix_field);
... some other stuff ...
node_save($node);

Comments

Joshua Brunner’s picture

Uhm, same problem heere.

Line 77 in matrix.module:
$additions = array($field['field_name'] => $values);

Should be:
$additions[$field['field_name']]['matrix'] = $values;

Never change a running system. If You change this line or add my one below the Original one, you have to fix your contemplates :(.

This Code should work without any changes at the matrix.module:

$node = node_load($nid);
dpr($node->field_matrix_field_field);
$node->$node->field_matrix_field_field['matrix'] = $node->field_matrix_field_field;
node_save($node);

Joshua

aaron1234nz’s picture

Status: Active » Needs review

Thanks Joshua,

I'll take a look

aaron1234nz’s picture

Status: Needs review » Fixed

another patch I committed has fixed this issue (5.x and 6.x branch)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.