My specific problem is similar to the one here but I am needing for it to work when the file field is within a node that is rendered within another as an entity reference.
If I use:
$node = \Drupal::request()->attributes->get('node');
$variables['node_title'] = $node->getTitle();
I get the title of the main node currently being displayed and not the node of the rendered entity reference. Is there a way I can get this instead?