Nov 15, 2019 in Drupal 8, Snippets

There is a great shortcut I use a lot when dealing with entity references (the're great, right) in drupal 8. I thought I'd share the snippet here.

$questions = $questionnaire->get('field_questions')->referencedEntities();
// $questionnaire is an entity type (f.e. node) with a field where you can reference multiple questions
// this returns the questions entities you can directly use as object
foreach($questions as $question) {
  // print $question->label();