Score:0

How can I attach a newly added paragraph to existing records?

uy flag

While editing a form, I needed to delete some fields and add a new paragraph. The issue here is, the newly added paragraph is not initialized in the edit form and that makes sense because the old data don't have that field so I have to initialize it manually by clicking on "Add Paragraph", then Save otherwise I won't be able to access it programmatically, but I have hundreds of users and I can't do that.

How can I add a paragraph to existing records?

I'm unable to use Drush on this server.

Score:0
cn flag

You add newly created paragraphs (without saving them) to the node field:

$node->field_paragraph[] = Paragraph::create([
  'type' => 'paragraph_type',
  'field_foo' => 'bar',
]);

and save them later together with the node:

$node->save();
id flag
I think the question is regarding how to do so in bulk and without Drush. So I think we need also to add at a minimum an entity query and an update hook.
Mourad Tlili avatar
uy flag
Yes ! it's working fine ! You saved me a lot of hours. Thank you
Mourad Tlili avatar
uy flag
@cilefen I think you are right! How to do that?
id flag
I am certain there are answers on this forum for that and all over the Internet. Example: https://www.thirdandgrove.com/insights/using-batch-api-and-hookupdaten-drupal-8/. If there are very many of these you may have to enqueue them, but that's only if the update is too time-consuming.
Mourad Tlili avatar
uy flag
The link you provided was exactly what I needed. Thanks a lot.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.