Drupal 8:如何编辑段落字段值?
Posted
技术标签:
【中文标题】Drupal 8:如何编辑段落字段值?【英文标题】:Drupal 8: How to edit Paragraph field value? 【发布时间】:2021-02-23 13:13:58 【问题描述】:我想知道是否可以设置段落的值。 在我的模块中,我使用了 entity_presave 函数,如果我尝试获取段落值,它工作正常,但如果我尝试设置或编辑一个值,它不会以某种方式工作。
function setParagraph($node)
$paragraph = $node->field_paragraphs->getValue();
// Loop through the result set.
foreach ($paragraph as $element)
$p = \Drupal\paragraphs\Entity\Paragraph::load($element['target_id']);
$foo = $p->field_foo>value;
$bar = $p->field_bar->value;
`$foo and $bar are getting filled with the right values`
but if i want to set a value nothing will work
$p->set('field_foo', $bar); //not working
$p->field_foo = $bar; //not working
$p->field_foo->value = $bar; notworking
//$p->set('field_steamconnect', $steamconnect);
【问题讨论】:
【参考方案1】:啊
$p->set('field_foo', $bar);
$p->save();
会起作用
【讨论】:
【参考方案2】:use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
$p = Paragraph::Create();
$p->set('field_foo', [
'field_1' => 'xxx',
'field_2' => 'yyy',
]);
$p->save();
$node = Node::Create();
...
$node->set('field_paragraph', [
'target_id' => $p->id(),
'target_revision_id' => $p->getRevisionId(),
]);
$node->save();
【讨论】:
以上是关于Drupal 8:如何编辑段落字段值?的主要内容,如果未能解决你的问题,请参考以下文章