Get entity reference node object from within rendered entity reference
You're setting $node to the current request. On any node/* route that will be the node in question, not other nodes included on the page.

Since it is an entity reference field, you can access it like (for example, in a preprocess):

$foo_id = $node-&gt;field_reference_name-&gt;value;
$referenced_node = Node::load($foo_id);

// or
$referenced_file = File::load($foo_id);


If $node-&gt;field_name-&gt;value returns the id (node id).

However it isn't really clear if you are talking about a file, or, a node.