2
votes

In my module, I have a custom entity LocationEntity and it uses a custom field type CoordinateItem which has 2 sub fields X, Y.

So what is the correct way of Drupal 8 to:

1) Render this LocationEntity in a twig template?

  • What I'm doing is just send the entity to twig template '#loc1' and manually render its each field value using getter method {{ loc1.getName() }}. It works but I'm sure this is not the correct way to do.

2) If question 1 has another correct way then how to have more than one templates for rendering the entity?

  • For example, one simple layout with less fields and one detail layout with all fields.

3) In the entity, I defined to use the custom field CoordinateItem. But now what to do with its getter and setter, because it's not a single sub field inside?

  • Getter if $this->get('coord') -> a FieldItemList object.
  • Getter if $this->get('coord')->value -> null.
  • Setter? What type is the parameter?

3) I wrote a formatter for CoordinateItem to show its sub fields. But again I don't know how to use this formatter to render this custom field in the main (entity) template?

  • Maybe this is related to question 1 and 2?
1

1 Answers

3
votes

You should use field formaters to render fields. This makes it possible to configure how each field should be rendered. It's the same for both base fields and config fields.

When you have a custom entity, you need to do this yourself in a preprocess function. You can take the node module as an example:

function template_preprocess_node(&$variables) {
  ...
  // Helpful $content variable for templates.
  $variables += array('content' => array());
  foreach (Element::children($variables['elements']) as $key) {
    $variables['content'][$key] = $variables['elements'][$key];
  }
  ...
}

This basically makes it possible to do {{ content }} in the template which then renders all of the fields.

This is what is considered best practice and is the most flexible and easy way to render entities, as you simply will use the field formaters that already exist for fields.