Skip to content
Snippets Groups Projects

Issue #3353650: Provide cache context for UserCountry condition

3 files
+ 66
2
Compare changes
  • Side-by-side
  • Inline
Files
3
+ 58
0
<?php
namespace Drupal\smart_ip\CacheContext;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CacheContextInterface;
use Drupal\smart_ip\SmartIpLocationInterface;
/**
* Defines the CountryCodeCacheContext service, for "by country code" caching.
*
* Cache context ID: 'smart_ip_location'.
*/
class CountryCodeCacheContext implements CacheContextInterface
{
/**
* Contains user's location.
*
* @var \Drupal\smart_ip\SmartIpLocationInterface
*/
protected $location;
/**
* Constructs a CountryCodeCacheContext.
*
* @param \Drupal\smart_ip\SmartIpLocationInterface $location
* Smart IP's data location.
*/
public function __construct(SmartIpLocationInterface $location) {
$this->location = $location;
}
/**
* {@inheritdoc}
*/
public static function getLabel()
{
return t('Cache context by country code.');
}
/**
* {@inheritdoc}
*/
public function getContext()
{
$user_country = $this->location->get('countryCode');
return \sha1($user_country);
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata()
{
return new CacheableMetadata();
}
}
Loading