How to access entity from EntityReferenceItem? I have the attached array below and need help figuring out how to access the 'entity' attribute that is under the protected "properties" array (see screenshot). 

I am using the Viewfield module



background:
field_name is an entity reference field. I need to get the EntityReference so that I can call getTarget() to get the EntityAdapter and then call EntityAdapter->getValue(). 

I have tried the following but they all give errors. The idea is to get the EntityReference which is the 'entity' value. How can i do this?

mytheme_preprocess_viewfield_item(&amp;vars) {

  $element = $vars['element'];
  $results = $element['#content']['#view']-&gt;result;

  foreach($results as $row) {
    $entity = $row-&gt;_entity
    $offset = $entity-&gt;get('field_name')-&gt;offsetGet(0)

kint($entity-&gt;get($video_field_name)) /* result of screenshot */

    $entityReference = $offset-&gt;get('entity') /* failed */
    $entityReference = $offset-&gt;getProperties() /* failed */
    $entityReference = $offset-&gt;getEntity() /* failed */
    $entityReference = $offset-&gt;getValue() /* failed */
  }//end foreach
}//end theme_preprocesss


The error message is: replace getValue() with any of the above

Call to a member function getValue() on null
If entity in the foreach loop is not empty you should be able to do

$entity-&gt;field_name-&gt;entity
//or perhaps
$entity-&gt;field_name-&gt;referencedEntities();