D7 compatibility

Feedback welcome. I tested this myself and it works as expected for both D7 and D8.