Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify WFO promo block to serve data from the WFO info content type #1555

Merged
merged 3 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Drupal\weather_blocks\Plugin\Block\Test;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\weather_blocks\Plugin\Block\WFOPromoBlock;

/**
Expand All @@ -23,7 +24,7 @@ final class WFOPromoBlockTest extends Base
}

/**
* Test that the block returns a node iD.
* Test that the block returns promo info
* @group unit
* @group block
* @group wfo-promo-block
Expand All @@ -32,16 +33,79 @@ final class WFOPromoBlockTest extends Base
{
$this->onLocationRoute();

$promo = (object) [
"field_wfo" => (object) [
"entity" => (object) [
"name" => (object) [
"value" => "WFO name",
],
"field_wfo_code" => (object) [
"value" => "WFO code",
],
],
],
"body" => "WFO information body",
"field_phone_number_opt" => "phone number entity",
"field_facebook_url" => "facebook url entity",
"field_twitter_url" => "twitter url entity",
"field_youtube_url" => "youtube url entity",
];

$this->entityService
->method("getLatestNodeFromWFO")
->willReturn($promo);

$expected = [
"name" => "WFO name",
"code" => "WFO code",
"about" => "WFO information body",
"phone" => "phone number entity",
"social" => [
"facebook" => "facebook url entity",
"twitter" => "twitter url entity",
"youtube" => "youtube url entity",
],
];

$actual = $this->block->build();

$this->assertEquals($expected, $actual);
}

/**
* Test that the block returns WFO name and code if there is no WFO info
* @group unit
* @group block
* @group wfo-promo-block
*/
public function testReturnsWFOIfNoPromo(): void
{
$this->onLocationRoute();

$promo = null;

$this->entityService->method("getLatestNodeFromWFO")->willReturn(null);

// Setup fetching the actual node.
$story = $this->createStub(ContentEntityInterface::class);
$wfoTaxonomyTerm = $this->createStub(ContentEntityInterface::class);

$story->method("id")->willReturn(94);
$wfoName = $this->createStub(TypedDataInterface::class);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another moment where I'm like "how did he figure this out?"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lord, I don't remember. I just copied it from elsewhere in the tests.

$wfoName->method("getString")->willReturn("WFO name");

$wfoTaxonomyTerm
->method("get")
->will($this->returnValueMap([["name", $wfoName]]));

$this->entityService
->method("getLatestNodeFromWFO")
->willReturn($story);
->method("getWFOEntity")
->willReturn($wfoTaxonomyTerm);

$expected = ["node" => 94];
$expected = [
"name" => "WFO name",
"code" => "WFO",
"phone" => false,
"social" => false,
];

$actual = $this->block->build();

Expand Down
45 changes: 42 additions & 3 deletions web/modules/weather_blocks/src/Plugin/Block/WFOPromoBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,56 @@ public function build()

$promo = $this->entityTypeService->getLatestNodeFromWFO(
$grid->wfo,
"wfo_promo",
"wfo_info",
);

// If we actually have a story, pass its ID on for the template
// If there is WFO info, pull out the parts that should be rendered
// in the WFO promo block on the forecast page.
if ($promo) {
$name = $promo->field_wfo->entity->name->value;
$code = $promo->field_wfo->entity->field_wfo_code->value;

$about = $promo->body;
$phone = $promo->field_phone_number_opt ?? false;

$facebook = $promo->field_facebook_url ?? false;
$twitter = $promo->field_twitter_url ?? false;
$youtube = $promo->field_youtube_url ?? false;

$social = false;
if ($facebook || $twitter || $youtube) {
$social = [
"facebook" => $facebook,
"twitter" => $twitter,
"youtube" => $youtube,
];
}
Comment on lines +44 to +51
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inspired by @eric-gade's comment in #1542


return [
"name" => $name,
"code" => $code,
"about" => $about,
"phone" => $phone,
"social" => $social,
];
}

// If there's not a WFO info, just return the name and code.
$taxonomyTerm = $this->entityTypeService->getWFOEntity($grid->wfo);
if ($taxonomyTerm) {
$name = $taxonomyTerm->get("name")->getString();

return [
"node" => $promo->id(),
"name" => $name,
"code" => $grid->wfo,
"phone" => false,
"social" => false,
];
}
}

// If we get here, it's because either the location doesn't have a grid
// point and thus no WFO, or else because we don't know about the WFO.
return [];
}
}
11 changes: 11 additions & 0 deletions web/modules/weather_data/src/Service/WeatherEntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,15 @@ public function getLatestNodeFromWFO($wfo, $nodeType)

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

public function getWFOEntity($wfo)
{
$term = $this->entityTypeManager
->getStorage("taxonomy_term")
->loadByProperties(["field_wfo_code" => $wfo]);
if (count($term) > 0) {
return array_pop($term);
}
return false;
}
Comment on lines +94 to +103
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

}
Original file line number Diff line number Diff line change
@@ -1 +1,28 @@
{{ drupal_entity("node", content.node) }}
<div class="grid-container padding-x-2">
<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 href="/offices/{{ content.code }}">{{ content.name }}</a></h4>
<p class="line-height-sans-4 margin-top-0">
{{ content.about | view }}
</p>

{% set hasPhone = content.phone | length %}
{% if hasPhone > 0 %}
{{ content.phone | view }}
{% endif %}

{% if content.social is not same as false %}
<p class="font-family-mono font-mono-sm text-bold text-primary-darker text-uppercase margin-bottom-1">
{{ "Find us on" | t }}
</p>
<ul class="usa-list usa-list--unstyled">
{{ content.social.facebook| view }}
{{ content.social.youtube | view }}
{{ content.social.twitter | view }}
</ul>
{% endif %}

</div>
</div>
</div>
Loading