I have created a computed field (computed_field module) and I'd like the calculated value of this field to be computed on the fly for each of my views results.
Specifically, I have a start time, and and end time for each "Offer" (content type) in my view. I have created a computed field that determines whether the current time is between the start time, and end time, and then outputs a string value of "now" or "ending_soon" or "starting_soon" or "default" (if it's outside the parameters). I'm doing this all because I need to use the contents of this calculated field as a path variable for custom map markers in a google map (different 'pins' for offers/deals that are happening now, vs ones that are happen earlier or later in the day).
inside my THEMENAME.theme file:
function computed_field_field_now_compute($entity_type_manager, $entity, $fields, $delta)
{
// from the database:
$timestamp = $fields['field_business_dates'][0]['value'] ?? "";
$endtimestamp = $fields['field_business_dates'][0]['end_value'] ?? "";
$now = gmdate('Y-m-d\TH:i:s\Z');
$utc = new DateTimeZone('UTC');
$startDate = new DateTime($timestamp, $utc);
$endDate = new DateTime($endtimestamp, $utc);
$nowDate = new DateTime($now, $utc);
$startSwatch = date_format($startDate, 'B');
$endSwatch = date_format($endDate, 'B');
$nowSwatch = date_format($nowDate,'B');
$cutOffSwatch = 22; // Note: 41.6 = 1 hour;
$endingSoonTime = $endSwatch - $cutOffSwatch;
$startingSoonTime = $startSwatch - $cutOffSwatch;
$value = "default";
if ($nowSwatch >= $startSwatch && $nowSwatch <= $endSwatch) {
if ($nowSwatch >= $endingSoonTime) {
$value = "ending_soon";
} else {
$value = "now";
}
} else if ($nowSwatch >= $startingSoonTime && $nowSwatch <= $startSwatch) {
$value = "starting_soon";
} else {
// return default value
}
return $value;
}
As these calculations are time-sensitive, I've turned off caching with the hope that the values are calculated on the fly and displayed in my view. But they are not displayed in views, and don't seem to display at all unless I save each individual node first. As I have over 6500 nodes...I don't see this as feasible. How can I ensure that when a view needs to display the calculated field, that the calculation is executed and rendered?