In creating a 'traffic light' field formatter, how to access views data stored in an entity 2 levels up?

The traffic light is 'green' if speed is 'good'. There is a radar entity which has the location and last reading (inherited structure). The radar might have more than one road associated with it.

The view queries roads. The display is content with a custom display type and layout manager. The 'traffic light formatter has access to the speed field as this is the field it is formatting. It has access to the radar entity passed to the field formatter. I can't see a way to access the road entity which has the speed limit field. The speed limit is required to work out whether the recorded speed is greater than the speed limit.

class RoadTrafficLightFormatter extends FormatterBase {

  public function viewElements(FieldItemListInterface $items, $langcode) {
    foreach ($items as $delta => $item) {

    protected function trafficLight(NodeInterface $radar) {
      $minimumLevel = $radar->get('field_gauge');
      // ? How to get the road that is referencing the traffic light to find the speed limit?


Two ways came to mind

  • views-pre-render event. Where to save the road nid?
  • views_field_formatter module allows you to define the view in the field formatter settings. Use this to load the view a second time?

1 Answer 1


Found the referring entity


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.