Use this code to generate a flag (created by the Flag module) against a particular content item, which is called a flagging entity. The "like_content" is the ID of the flag you created. The "entity_type" is the type of entity being flagged.
$flaggingStorage = \Drupal::service('entity_type.manager')->getStorage('flagging');
$flagging = $flaggingStorage->create([
'flag_id' => 'like_content'
'entity_type' => 'page',
'entity_id' => 1,
'uid' => 1,
'created' => time(),
'session_id' => NULL,
]);
$flagging->save();
Add new comment