I've tinkered around a little and created a modifier for the {{ wind_speed }} field. This replaces the need i.e. for a custom {{speed_bft}} field.
Now you can convert to speed units on the fly like so:
{{ wind_speed | unit:{unit} }}
replace {unit} with one of the following to convert or remove the modifier for default units
for metric units (no ms since it's the default)
for imperial units (no mph since it's the default)
To register it in ServiceProvider.php just add
use Vannut\StatamicWeather\Modifiers\ConvertSpeedToUnitModifier;
protected $modifiers = [
ConvertSpeedToUnitModifier::class,
];
And for the modifier itself create the file src\Modifiers\ConvertSpeedToUnitModifier.php
<?php
namespace Vannut\StatamicWeather\Modifiers;
use Statamic\Modifiers\Modifier;
use Vannut\StatamicWeather\Settings;
class ConvertSpeedToUnitModifier extends Modifier
{
protected static $handle = 'unit';
public function index($value, $params, $context)
{
// Get the default units set in the control panel from which to convert
$config = (new Settings)->get();
$currentUnits = $config->get('units','metric');
$targetUnits = '';
$result;
if ($param = array_get($params, 0)) {
// Either get the variable from the context, or if it doesn't exist,
// use the parameter itself - we'll assume its a number.
$targetUnits = array_get($context, $param, $param);
}
if($currentUnits == 'metric')
{
if($targetUnits=='kmh')
{
$result=round($value*3.6,2);
}
elseif ($targetUnits=='mph'){
$result=round($value*2.237,2);
}
elseif($targetUnits=='bft')
{
if ($value < 0.3)
{
return 0;
} else if ($value < 1.6)
{
return 1;
} else if ($value < 3.4)
{
return 2;
} else if ($value < 5.5)
{
return 3;
} else if ($value < 8)
{
return 4;
} else if ($value < 10.8)
{
return 5;
} else if ($value < 13.9)
{
return 6;
} else if ($value < 17.2)
{
return 7;
} else if ($value < 20.8)
{
return 8;
} else if ($value < 24.5)
{
return 9;
} else if ($value < 28.5)
{
return 10;
} else if ($value < 32.6)
{
return 11;
} else
{
return 12;
}
}
}
else {
if($targetUnits=='kmh')
{
$result=round($value*1.609,2);
}
elseif ($targetUnits=='ms'){
$result=round($value/2.237,2);
}
elseif($targetUnits=='bft')
{
if ($value < 1)
{
return 0;
} else if ($value < 4)
{
return 1;
} else if ($value < 8)
{
return 2;
} else if ($value < 13)
{
return 3;
} else if ($value < 19)
{
return 4;
} else if ($value < 25)
{
return 5;
} else if ($value < 32)
{
return 6;
} else if ($value < 39)
{
return 7;
} else if ($value < 47)
{
return 8;
} else if ($value < 54)
{
return 9;
} else if ($value < 64)
{
return 10;
} else if ($value < 73)
{
return 11;
} else
{
return 12;
}
}
}
return $result;
}
}
It's nothing too fancy, but it works :)
I've tinkered around a little and created a modifier for the
{{ wind_speed }}field. This replaces the need i.e. for a custom{{speed_bft}}field.Now you can convert to speed units on the fly like so:
{{ wind_speed | unit:{unit} }}replace
{unit}with one of the following to convert or remove the modifier for default unitsfor metric units (no ms since it's the default)
kmhmphbftfor imperial units (no mph since it's the default)
kmhmsbftTo register it in
ServiceProvider.phpjust addAnd for the modifier itself create the file
src\Modifiers\ConvertSpeedToUnitModifier.phpIt's nothing too fancy, but it works :)