I created a custom entity (submenu) in Drupal 8. I need to render this custom entity in my custom module. This is the code I am using.
$entity_type = 'submenu';
$entity_id = '1'; // static for example purpose
$view_mode = 'full';
$entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id);
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('submenu');
$pre_render = $view_builder->view($entity, $view_mode);
$render_output = render($pre_render);
I keep getting the following error.
TypeError: Argument 1 passed to Drupal\Core\Entity\EntityViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface, null given
Why do I get this error?