Sep 18, 2020 in Drupal 8, Snippets

There are some ways to make sure your loaded object is in fact an entity of type node. I prefer the following way. In addition I check if the entity types are correct too.

$view_builder = \Drupal::entityTypeManager()->getViewBuilder($entity_type);
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
$node = $storage->load(MY_NODE_ID);
$allowed_types = ['province', 'contact_person'];

if($node instanceof NodeInterface && in_array($node->getType(), $allowed_types)) {
  // Here, we are sure we have a correct entity of type province or contact person
}