How to get fields of media_entity? We have a view that returns a Media_entity (Drupal\media_entity\Entity\Media) using the following in a hook_preprocess_view_views hook. When we try to access the field_media_type variable, I get a NULL. field_media_type is a custom field (taxonomy field) on all the Media bundles.

How do we get the fields from a Media_entity?

use Drupal\media_entity\Entity\Media; 

function mymodule_preprocess_views_view(&amp;$variables) {
  $results = $variables['content']['#view']-&gt;result;
  foreach($results as $row){ 
    $media_entity = $row-&gt;_entity;
    ksm($media_entity-&gt;get('field_media_type'); // returns null.
  }
}


I also tried the following:

use Drupal\media_entity\Entity\Media; 

function mymodule_preprocess_views_view(&amp;$variables) {
  $results = $variables['content']['#view']-&gt;result;
  foreach($results as $row){ 
    $mid = $row-&gt;mid;
    $media_entity = Media::load($mid);
    ksm($media_entity); // null
  }
}


content array


iterator contents
An alternative solution I found was to use get the target_id and use Term::load($term_id)-&gt;getName() to get the value we wanted.  

use Drupal\media_entity\Entity\Media; 
use Drupal\taxonomy\Entity\Term;

function mymodule_preprocess_views_view(&amp;$variables) {
  $results = $variables['content']['#view']-&gt;result;
  foreach($results as $row){ 
    $media_entity = Media::load($row-&gt;mid);
    $media_target_id = $media_entity-&gt;get('field_media_type')-&gt;target_id;
    $media_type_name = Term::load($media_target_id)-&gt;getName();
  }
}