Score:0

How to programmatically add a paragraph item to a user account?

cn flag

I already added the Check in/out History field to the user account, which has the check-in date and check-out date fields.

How can I programmatically add new items to Check in/out History?

I need to create some history for users.

Check-in date Check-out date
04/05/21 05/07/21
03/05/20 04/14/20
leymannx avatar
ne flag
Quite broad the question. What have you tried so far? Did you check for "programmatically create paragraph items" or similar? User entity is just another fieldable entity. Same as nodes. Most answers you'll find are for nodes. You just need to replace the node object with an user object then, done. When and where do you want to update the users? API call? Some hook that gets triggered in a particular situation?
Ruzanna Hovasapyan avatar
cn flag
for example I have field_history as paragraph in user account, and there are 2 fields in paragraph field_checkin_date, field_checkout_date, and I want on some action add new paragraph items, what should be my steps? 1. how reach paragraph field for user? 2. how add new paragraph item with 2 sub fields? 3. how append new item to paragraph?
Ruzanna Hovasapyan avatar
cn flag
Thanks to all, this code solve my task:
Ruzanna Hovasapyan avatar
cn flag
$paragraph = Paragraph::create(['type' => 'checkin_history', 'field_checkin' => $checkin_date, 'field_checkout' => format_date(time(), 'html_date')]); $paragraph->save(); $current = $account->get('field_check_in_out_history')->getValue(); $current[] = array( 'target_id' => $paragraph->id(), 'target_revision_id' => $paragraph->getRevisionId(), ); $account->set('field_check_in_out_history', $current); $account->save();
Ruzanna Hovasapyan avatar
cn flag
where: //field_check_in_out_history -> field in user account //checkin_history -> paragraph type // field_checkin and field_checkout paragraph fields
leymannx avatar
ne flag
Please update the question for clarification and to add details, please don't use comments for that. Next reader should get all relevant info at one glance.
leymannx avatar
ne flag
And then please add an answer with your solution, and accept it, to help future readers to get the solution at one glance.
Score:0
cn flag

I have been able to achieve the task using the following code.

$paragraph = Paragraph::create([
  'type' => 'checkin_history',
  'field_checkin' => $checkin_date,
  'field_checkout' => format_date(time(), 'html_date')
]); 
$paragraph->save(); 

$current = $account->get('field_check_in_out_history')->getValue(); $current[] = [
  'target_id' => $paragraph->id(),
  'target_revision_id' => $paragraph->getRevisionId(),
]; 

$account->set('field_check_in_out_history', $current); 
$account->save();
  • field_check_in_out_history is the user account field
  • checkin_history is the paragraph type
  • field_checkin and field_checkout are paragraph fields
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.