[英]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.我每天都在尝试将数据导入 Drupal 8,但在需要更新图像时被阻止。 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.当我第一次导入图像时,我正在创建它们并将它们添加到节点 field_images 字段。 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:
当我重新加载这个更新的节点时,看到这个调试器图像:
How could I update all translations too ?我怎样才能更新所有翻译?
You need to do something like this:你需要做这样的事情:
'field_image' =>
[
'target_id' => $values['image'][0],
'alt' => $values['title'],
],
尝试这个
$node->field_images->de->target_id = $file->id();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.