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:

$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);

To render all entities of a custom entity, do the following:

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