Skip to content

Commit

Permalink
Merge pull request #1804 from weather-gov/eg-1681-alaska-afds
Browse files Browse the repository at this point in the history
Fix Anchorage WFO Promo links
  • Loading branch information
eric-gade authored Sep 23, 2024
2 parents f55e42c + 23da136 commit f739e53
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 4 deletions.
19 changes: 17 additions & 2 deletions web/modules/weather_data/src/Service/WeatherEntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,20 @@ public function getLatestNodeFromWeatherEvent($eventType, $nodeType)
public function getLatestNodeFromWFO($wfo, $nodeType)
{
// Get the ID for the WFO taxonomy term that matches our grid WFO.
$wfoCode = $this->normalizeAnchorageWFO($wfo);
$termID = $this->entityTypeManager
->getStorage("taxonomy_term")
->loadByProperties(["field_wfo_code" => $wfo]);
->loadByProperties(["field_wfo_code" => $wfoCode]);

return $this->getLatestNodeByTerm($termID, "field_wfo", $nodeType);
}

public function getWFOEntity($wfo)
{
$wfoCode = $this->normalizeAnchorageWFO($wfo);
$term = $this->entityTypeManager
->getStorage("taxonomy_term")
->loadByProperties(["field_wfo_code" => $wfo]);
->loadByProperties(["field_wfo_code" => $wfoCode]);
if (count($term) > 0) {
return array_pop($term);
}
Expand All @@ -115,4 +117,17 @@ public function getWFOEntities()
->loadMultiple($ids);
return $result;
}

/**
* Normalize WFOs for Alaska/Anchorage
*/
public function normalizeAnchorageWFO(string $wfo): string
{
$inAnchorageCodes = ["aer", "alu"];
$matches = in_array(strtolower($wfo), $inAnchorageCodes);
if ($matches) {
return "AFC";
}
return $wfo;
}
}
45 changes: 45 additions & 0 deletions web/modules/weather_data/src/Service/WeatherTwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Drupal\weather_data\Service;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\Extension\ExtensionInterface;

class WeatherTwigExtension extends AbstractExtension
{
public function getFunctions()
{
return [
new TwigFunction(
"normalize_wfo",
[$this, "normalizeWFO"],
["is_safe" => ["html"]],
),
];
}

public function getFilters()
{
return [new TwigFilter("normalize_wfo", [$this, "normalizeWFO"])];
}

/**
* Handle the edge cases of WFO codes
* for Alaska/Anchorage
*/
public function normalizeWFO($wfo)
{
if (!$wfo) {
return "";
}
$anchorageAlternates = ["alu", "aer"];
$isAlternate = in_array(strtolower($wfo), $anchorageAlternates);
if ($isAlternate) {
return "AFC";
}

return $wfo;
}
}
4 changes: 4 additions & 0 deletions web/modules/weather_data/weather_data.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ services:
newrelic_metrics:
class: Drupal\weather_data\Service\NewRelicMetrics
arguments: ['@http_client']
weather_twig_extension:
class: Drupal\weather_data\Service\WeatherTwigExtension
tags:
- { name: twig.extension }
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<div class="grid-row">
<div class="grid-col tablet-lg:grid-offset-2 tablet-lg:grid-col-8">
<h3 class="wx-visual-h2 text-normal text-primary-darker"> {{ "Your Local Forecast Office" | t }} </h3>
<h4 class="wx-visual-h3 margin-bottom-1"><a class="usa-link" href="/offices/{{ content.code }}">{{ content.name }}</a></h4>
<h4 class="wx-visual-h3 margin-bottom-1"><a class="usa-link" href="/offices/{{ content.code | normalize_wfo }}">{{ content.name }}</a></h4>
<p class="line-height-sans-4 margin-top-0">
{{ content.about | view }}
</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<use
xlink:href="/{{ directory }}/assets/images/uswds/sprite.svg#info_outline"
></use>
</svg><a href="/afd/{{ weather.grid.wfo }}" class="usa-link">Area forecast discussion</a>
</svg><a href="/afd/{{ weather.grid.wfo | normalize_wfo }}" class="usa-link">Area forecast discussion</a>
</div>
</div>
</div>
Expand Down

0 comments on commit f739e53

Please sign in to comment.