Defining a new field formatter
Looking at the full version of a trip node, you’ll see that field_lat_lng
is a little bit ugly. It prints the raw values for the latitude and longitude. It should be way better to have a geographical map with a marker on it. We can implement this directly in a Twig template, but this solution has a couple of drawbacks:
- Twig is quite limited when it comes to data manipulation
- If we need a similar field but with a different name, we have to duplicate the code
Providing a different way to render a field on the frontend is a task for a field formatter.
First of all, we need to define a new theme hook in a custom module:
function alps_base_theme(): array { return [ 'alps_base_lat_lng' => [ 'variables' => [ 'lat' => '', ...