1

I have a requirement to create/update a paragraph which is added as a custom field in USER profile. After surfing here, i got how to update a paragraph which is connected to node. But I am not able figure out for User profile. Any help will be appreciated. I used below code for saving the paragraph, but how it will linked to $user->save(); ?

$paragraph = Paragraph::create([
      'type' => 'mmodule_paragraph',
      'field_employee_id' => 'Field value',
      'field_location' => 'Field value',
      'field_name' => 'Field value',
      'field_segment' => 'Field value',
    ]);
    $paragraph->save();
4

Don't save the paragraph, add it to the user field and then save it together with the user:

$paragraph = Paragraph::create([
  'type' => 'mmodule_paragraph',
  'field_employee_id' => 'Field value',
  'field_location' => 'Field value',
  'field_name' => 'Field value',
  'field_segment' => 'Field value',
]);
$user->field_paragraph[] = $paragraph;
$user->save();

In case you want to replace the field content overwrite the entire field array:

$user->field_paragraph = [['entity' => $paragraph]];
  • Thanks. Too small.. too good.. – Suraj Sep 24 '19 at 8:49
  • Hi @4k4, I have one more query regarding same. Before adding values to this field i need to delete if any values exists. Can you please help in this regard? – Suraj Sep 24 '19 at 11:24
  • The field works like a php array, see the revised answer. – 4k4 Sep 24 '19 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.