0

Will try to be brief.

I have a node with "n" images asigned to it, now i want to add new images (and keep the currents) to the node, i've tried this but no success until now. The next code does nothing:

$node = Node::load($node_id);
$node->field_car_picture[] = $new_images;
$node->save();

Also tried this and removes the current pictures and leave only the new images:

$node = Node::load($node_id);
$node->field_car_picture->setValue($new_images);
$node->save();

Any help will be appreciated

2

You can try to merge the existing field items array and the new one, but I would use a loop to add one image at a time:

$node = Node::load($node_id);
foreach ($new_images as $new_image) {
  $node->field_car_picture[] = $new_image;
}
$node->save();

If you want to merge the arrays get the existing one

$existing_images = $node->field_car_picture->getValue();

combine it with the new array and replace the fields content with it.

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.