Skip to content

Instantly share code, notes, and snippets.

@laradevitt
Last active September 24, 2017 22:42
Show Gist options
  • Save laradevitt/3afc6fbb4ee04b2f33dbfc093f22967f to your computer and use it in GitHub Desktop.
Save laradevitt/3afc6fbb4ee04b2f33dbfc093f22967f to your computer and use it in GitHub Desktop.
(Drupal 8.x) Programmatically load entities by type and build an HTML string consisting of teaser views.
<?php
// Note that I'm building an HTML string for illustrative purposes only since
// best practice is to allow the twig templates to build HTML from the
// structured render arrays.
$html = '';
$storage = \Drupal::entityManager()->getStorage('notice');
$result = \Drupal::entityQuery('notice')
->execute();
$notices = $storage->loadMultiple($result);
if (!empty($results)) {
$view_builder = \Drupal::entityManager()->getViewBuilder('notice');
foreach ($notices as $entity) {
$render_array = $view_builder->view($entity, 'teaser');
$html .= \Drupal::service('renderer')->renderPlain($render_array);
}
}
kint($html);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment