Reputation: 23
i wondering if i can set a value of a paragraph. in my Module i use a entity_presave function and if i try to get the Paragraph values its working fine but if i try to set or edit a value its not working somehow.
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);
}
}
Upvotes: 1
Views: 3843
Reputation: 174
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();
Upvotes: 2