如何解决以编程方式更新 Drupal 8 上的节点字段
我正在使用 Drupal 8 并尝试以编程方式更新以下节点的字段:
- 已保存
- 未发布
我希望能够同时发布多个节点,并运行我的钩子,以编程方式向所有新发布的节点添加标准值。
我在这里查看了解决方案: How to manipulate value before node is saved in Drupal 8? ...和这里: https://drupal.stackexchange.com/questions/304363/add-a-hero-image-and-text-when-a-node-goes-from-unpublished-to-published
...但是当我实施这些建议时(例如在我下面的代码中),我的内容节点上的字段没有更新,它们保持为空。
如果你知道我如何更新这个,请指教。谢谢。
模块结构:
module_name
module_name.info
module_name.module
module_name.module:
<?PHP
namespace Drupal\Core\Field\EntityReferenceFieldItemList;
namespace Drupal\node\Entity;
//In the function below,I'm attempting to update
//'event' type nodes,specifically those with an event_type of '30'
//In those nodes,I'm attempting to use a Media library image
//and use the page's title in the hero section
function module_name_node_presave(Drupal\node\NodeInterface $entity){
if ($entity->bundle() === 'event' && $entity->get('field_event_type')->toString() === '30') {
if ($entity->get('field_hero_tagline')->isEmpty()) {
$entity->set('field_hero_tagline',$entity->label());
}
if ($entity->get('field_hero_image')->isEmpty()) {
$media = Media::load(53);
$entity->set('field_hero_image',$media);
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。