Mar 23, 2017 in Snippets, Drupal 8

In this case I created a custom entity called 'submenu'. In my custom module a used HOOK_LINK_ALTER to make the submenu (which contains a textfield) visible on hover. I used the code below to render my custom entity content:

1) Render the output of a custom entity programmatically

$entity_type = 'submenu';
$entity_id = $entity_item_id;
$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);

2) Render object of a custom entity programmatically 

$storage = \Drupal::entityTypeManager()->getStorage('submenu');
$ids = \Drupal::entityQuery('submenu')->execute();
$apiList = $storage->loadMultiple($ids);