Drupal 10: Creating A Flagging Entity

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

The content of this field is kept private and will not be shown publicly.
CAPTCHA
2 + 0 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.