0

I have a content type with an entity reference field to another content type. We'll call the first content type A, and the other B.

In my node level template for A, I need to output a google_map_field (https://www.drupal.org/project/google_map_field) from referenced entity (B).

I'm outputting other fields from B by using:

node.field_entity_reference.entity.field_example.value

However, this does not work for the google_map_field. Lots of errors spit out, regarding parts of the google_map_field, such as lat, lng etc. I'm guessing this is because the module usually does some processing of this field, which isn't being done due to the nature of how I'm outputting it?

I need to work out how to tell Drupal to render it as a GoogleMapFieldType (I beleive).

Any guidance on this would be highly appreciated!

Thanks in advance.

2 Answers 2

1
+50

The most out of the box way to display a referenced entity is this:

1) Create a custom view mode "google_map".

2) Set field_entity_reference to render the entity with that view mode.

3) Output the field from the content variable, like this:

 {{ content.field_entity_reference }}

In your case, as you're pulling raw values out in your question, it looks like you want the control that comes from having all of your variables (including those from a referenced entity) in one template.

You can do this in a preprocess hook. For example in hook_preprocess_node() in your custom theme or module, you can assign the google map from the referenced entity to a variable:

 if ($node->field_entity_reference->isEmpty() === FALSE) {

    $referenced_entity = $node->field_entity_reference->referencedEntities()[0];

    $vars['google_map'] = $referenced_entity->field_example->view();

  }

If you want to pass a view module in to the view() method, you can do it like this:

$vars['google_map'] = $referenced_entity->field_example->view('default');

or

$vars['google_map'] = $referenced_entity->field_example->view('google_map');

Then in your twig, you would just access it like this:

{{ content.google_map }}

If you want to pull other raw values out of your referenced entity, you can access them easily like this:

$vars['referenced_entity_field_value'] = $referenced_entity->field_example_description->value;

But if you do this, you lose the formatting from a field formatter. For plain text fields, that's may be fine for your use case. But it's something to watch out for.

1

I would recommend using a view mode to render out the referenced entity.

For instance, A's display settings is set to display B using the 'teaser' view mode. Then, you can configure B's teaser display to show what you want.

I know this doesn't answer your direct referenced entity value question, but this is how I would tackle this problem.

I hope that helps!

2
  • Just to clarify, I'm not using a view (sorry if I wasn't clear). If you're talking about adjusting the display settings of either A or B content types, that's also not going to help. The issue arises just to the type of the field. Commented Apr 25, 2017 at 8:34
  • Daniel, he's talking about "view modes", not "views", see /admin/structure/display-modes/view on your Drupal installation, and /admin/structure/types/manage/article/display. See where it says default, RSS, teaser? Those are "view modes". You can select which field formatters and which fields you want to display per view mode.
    – oknate
    Commented Jun 27, 2017 at 18:01

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.