Last active
September 24, 2017 22:42
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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