How do I get a list of all taxonomy terms assigned to a node (or any entity)? What's the best way to get all taxonomy terms assigned to a node?

Should one use the taxonomy_index table?

Should one iterate through the fields on the node object?

I saw some answers for Drupal 7, but not for Drupal 8.
One way to do it is to iterate through each field and check if it's an entity_reference field with target type taxonomy_term, then extract the data you need.

$terms = [];

foreach ($node-&gt;getFields() as $key =&gt; $field) {
  if ($field-&gt;getFieldDefinition()-&gt;getType() == 'entity_reference') {
    $targetType = $field-&gt;getFieldDefinition()
      -&gt;getItemDefinition()
      -&gt;getSetting('target_type');
    if ($targetType == 'taxonomy_term') {
      $new_terms = array_map(function (\Drupal\taxonomy\TermInterface $term) {
        return $term-&gt;label();
      }, $field-&gt;referencedEntities());
      $terms = array_merge($terms, $new_terms);
    }
  }
}


One advantage to this approach is it will work with any fieldable entity type.