YoyoS
YoyoS

Reputation: 669

Edit images in a Drupal node programmatically

I'm trying to import data into a Drupal 8 everyday and I'm blocked at the point where I need to update images. So I've got a Image field which contains unlimited images.

When I first import images, I'm creating them and adding them to the node field_images field. The adding is working.

$entity = Node::create(
 [...,
  field_images => [$file1],
  ...
 ]);

When there's a node update, I need to replace every changed value of this node. This code is working perfectly

$node = Node::load(1);
$node->set('field_images',[$file2]);
$node->save();

Now this doesn't work when I enable translations as it edits only the default value. See this debugger image when I reload this updated node:

enter image description here

How could I update all translations too ?

Upvotes: 1

Views: 1511

Answers (2)

Nazmus Sakib
Nazmus Sakib

Reputation: 92

Try this

$node->field_images->de->target_id = $file->id();

Upvotes: 0

Arnold PÉTER
Arnold PÉTER

Reputation: 146

You need to do something like this:

     'field_image' => 
      [
        'target_id' => $values['image'][0],
        'alt' => $values['title'],
      ],

Upvotes: 1

Related Questions