问题描述
我正在使用 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 (将#修改为@)