简体   繁体   English

以编程方式编辑 Drupal 节点中的图像

[英]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.

相关问题 如何以编程方式设置节点 > 图像实体引用字段值,其中我在 drupal 8 节点编辑表单中具有 fid 值 - How to programmatically set node > Image entity reference field value where I have fid value in drupal 8 node edit form Drupal 8. 编辑节点页面上的评论字段 - Drupal 8. Comments field on edit node page Drupal 8:以编程方式更新节点不会更新视图中节点的状态 - Drupal 8: updating node programmatically does not update the state of the node in a view Drupal 8:以编程方式创建节点并使用表单 api 重定向到它 - Drupal 8: programmatically create node and redirect to it using form api Drupal 8以编程方式渲染节点时使用建议的模板 - Drupal 8 use suggested template when programmatically rendering node drupal 8以编程方式创建节点只能成功一次 - drupal 8 programmatically create node can only succeeded once Drupal 8 - 如何以编程方式创建节点实体引用变量? - Drupal 8 - How to programmatically create a node entity reference variable? 如何以编程方式将新创建的节点添加到 Drupal 组模块中的组中? - How to programmatically add newly created node to a group in the Drupal Group Module? 在Drupal 8中主题化编辑表单 - Theming the edit form in Drupal 8 Drupal 8 以编程方式创建字段 - Drupal 8 create field programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM