RobertBoes/nova-slider-field

How to run a closure in formatter method?

phuclh opened this issue · 1 comments

I have a function to convert minutes to hour format: 1 hour 25 minutes

<?php

function convertToHoursMins($time, $format = '%02d:%02d') {
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);
    return sprintf($format, $hours, $minutes);
}

echo convertToHoursMins(250, '%02d hours %02d minutes'); // should output 4 hours 17 minutes

How can I pass above function to formatter method to display in the slider label?

Thank you

The way this package currently works, it's not really possible. The data is sent 'raw' to the frontend, by Laravel Nova. There is an option to change the "display template", you can append an percentage sign for example. There's currently no way to transformer the data, but I'm open to pull requests! I've written this when Nova was just released, don't know if there was (or is now) an option to transform the data server side. I could add an extra meta field, which accepts a value or closure and use that for displaying the value. But I'll have to look into that