1

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?

1
  • How is this theming?
    – No Sssweat
    Commented Mar 15, 2017 at 13:26

1 Answer 1

0

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->field_reference_name->value;
$referenced_node = Node::load($foo_id);

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

If $node->field_name->value returns the id (node id).

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

2
  • Im not sure but i think $node->get('fieldname')->entity works.
    – user21641
    Commented Mar 15, 2017 at 13:46
  • This worked for that specific case but didn't work when the entity reference node wasn't a field of node being referred to in the route request. Though this does indeed answer how to get the entity reference node object and has been really helpful for something else I am trying to do so thank you very much! Commented Mar 15, 2017 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.