How to create an Entity in Drupal?
In order to create an entity in Drupal 8 in a general case we need to Entity type manager to get a storage for a specific entity type, for example node
// Use the entity manager to get node storage.
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
// Call create method passing values for a new node entity.
$node_storage->create([
'type' => 'page',
'title' => 'Page title',
]);
Or we can write it shorter
$node = \Drupal::entityTypeManager()->getStorage('node')->create(['type' => 'page', 'title' => 'Page title']);
We can also call a wrapper - a function, which provides the same functionality. I don't recommend to use it, because it is deprecated and can be removed at any moment
$node = entity_create('node', [
'type' => 'page',
'title' => 'Page title',
'body' => 'This a text of your page.',
]);
We can also call directly call method create of a specific entity
$node = Node::create([
'type' => 'page',
'title' => 'The page title',
]);