Skip to content

Commit

Permalink
adding cache tags to response
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfinnarn committed Jan 23, 2024
1 parent 5779970 commit bb599b8
Showing 1 changed file with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\va_gov_api\Controller;

use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Path\PathMatcherInterface;
Expand Down Expand Up @@ -73,13 +74,17 @@ public function bannerAlertsByPath(Request $request) {
return new JsonResponse(['error' => 'No path provided.']);
}

$banners = $this->collectBannerData($path);
$promo_banners = $this->collectPromoBannerData($path);
$full_width_banner_alerts = $this->collectFullWidthBannerAlertData($path);
[$banners, $banner_cache_tags] = $this->collectBannerData($path);
[$promo_banners, $promo_cache_tags] = $this->collectPromoBannerData($path);
[$full_width_banner_alerts, $full_width_banner_alert_cache_tags] = $this->collectFullWidthBannerAlertData($path);

return new JsonResponse([
$response = new CacheableJsonResponse([
'data' => array_merge($banners, $promo_banners, $full_width_banner_alerts),
]);

$cache_tags = array_merge($banner_cache_tags, $promo_cache_tags, $full_width_banner_alert_cache_tags);
$response->getCacheableMetadata()->addCacheTags($cache_tags);
return $response;
}

/**
Expand Down Expand Up @@ -117,10 +122,13 @@ protected function collectBannerData(string $path) {

// Add the banners to the response.
$banner_data = [];
$cache_tags = [];
foreach ($banners as $entity) {
$banner_data[] = $this->serializer->normalize($entity);
$cache_tags = array_merge($cache_tags, $entity->getCacheTags());
}
return $banner_data;

return ['data' => $banner_data, 'cache_tags' => $cache_tags];
}

/**
Expand Down Expand Up @@ -158,10 +166,12 @@ protected function collectPromoBannerData(string $path) {

// Add the promo_banners to the response.
$promo_banner_data = [];
$cache_tags = [];
foreach ($promo_banners as $entity) {
$promo_banner_data[] = $this->serializer->normalize($entity);
$cache_tags = array_merge($cache_tags, $entity->getCacheTags());
}
return $promo_banner_data;
return ['data' => $promo_banner_data, 'cache_tags' => $cache_tags];
}

/**
Expand Down Expand Up @@ -228,18 +238,20 @@ protected function collectFullWidthBannerAlertData(string $path) {

// Add the banners to the response.
$full_width_banner_alert_data = [];
$cache_tags = [];
foreach ($facility_banners as $entity) {
$full_width_banner_alert_data[] = $this->serializer->normalize($entity);
$full_width_banner_alert_data[] = $this->serializer->normalize($entity, 'json', ['plugin_id' => 'entity']);
$cache_tags = array_merge($cache_tags, $entity->getCacheTags());

// Override field_situation_updates with referenced paragraph data.
$situation_updates = $entity->get('field_situation_updates')->referencedEntities();
$situation_update_data = [];
foreach ($situation_updates as $situation_update) {
$situation_update_data[] = $this->serializer->normalize($situation_update);
$situation_update_data[] = $this->serializer->normalize($situation_update, 'json', ['plugin_id' => 'entity']);
}
$full_width_banner_alert_data['field_situation_updates'] = $situation_update_data;
$full_width_banner_alert_data[count($full_width_banner_alert_data) - 1]['field_situation_updates'] = $situation_update_data;
}
return $full_width_banner_alert_data;
return ['data' => $full_width_banner_alert_data, 'cache_tags' => $cache_tags];
}

}

0 comments on commit bb599b8

Please sign in to comment.