Skip to content

Commit

Permalink
OPENEUROPA-1414: Minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
upchuk committed Jan 15, 2019
1 parent 7c0dc48 commit ba92047
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class WebtoolsAnalyticsRule extends ConfigEntityBase implements WebtoolsAnalytic
protected $section = '';

/**
* The Webtools Analytics rule applicable for multilingual aliases.
* Indicates if the rule should be applied on the default site language alias.
*
* @var bool
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Drupal\oe_webtools_analytics\AnalyticsEventInterface;
use Drupal\oe_webtools_analytics_rules\Entity\WebtoolsAnalyticsRuleInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;

/**
* Event subscriber for the Webtools Analytics event.
Expand All @@ -28,19 +27,12 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface {
*/
protected $entityTypeManager;

/**
* The request stack.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;

/**
* The current path service.
*
* @var \Drupal\Core\Path\CurrentPathStack
*/
protected $currentPathStack;
protected $currentPath;

/**
* The alias manager service.
Expand Down Expand Up @@ -68,9 +60,7 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface {
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
* The request stack.
* @param \Drupal\Core\Path\CurrentPathStack $currentPathStack
* @param \Drupal\Core\Path\CurrentPathStack $currentPath
* The current path service.
* @param \Drupal\Core\Path\AliasManager $aliasManager
* The alias manager service.
Expand All @@ -79,10 +69,9 @@ class WebtoolsAnalyticsEventSubscriber implements EventSubscriberInterface {
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
* The Config Factory service.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, RequestStack $requestStack, CurrentPathStack $currentPathStack, AliasManager $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, CurrentPathStack $currentPath, AliasManager $aliasManager, CacheBackendInterface $cache, ConfigFactoryInterface $config) {
$this->entityTypeManager = $entityTypeManager;
$this->requestStack = $requestStack;
$this->currentPathStack = $currentPathStack;
$this->currentPath = $currentPath;
$this->aliasManager = $aliasManager;
$this->cache = $cache;
$this->siteConfig = $config->get('system.site');
Expand All @@ -96,19 +85,16 @@ public function __construct(EntityTypeManagerInterface $entityTypeManager, Reque
*/
public function analyticsEventHandler(AnalyticsEventInterface $event): void {
$event->addCacheTags(['webtools_analytics_rule_list']);
$current_path = $this->requestStack->getCurrentRequest()->getPathInfo();
$current_path = $this->currentPath->getPath();
$cache = $this->cache->get($current_path);
if ($cache && $cache->data === NULL) {
// If there is no cached data there is no section that applies to the uri.
return;
}

if ($cache = $this->cache->get($current_path)) {
// Set site section from the cached data.
if (isset($cache->data['section'])) {
$event->setSiteSection($cache->data['section']);
return;
}
if (isset($cache->data['section'])) {
$event->setSiteSection($cache->data['section']);
return;
}

$rule = $this->getRuleByPath($current_path);
Expand Down Expand Up @@ -138,7 +124,7 @@ protected function getRuleByPath(string $path): ?WebtoolsAnalyticsRuleInterface
foreach ($rules as $rule) {
$current_path = $path;
if ($rule->matchOnSiteDefaultLanguage()) {
$current_path = $this->aliasManager->getAliasByPath($this->currentPathStack->getPath(), $this->siteConfig->get('default_langcode'));
$current_path = $this->aliasManager->getAliasByPath($this->currentPath->getPath(), $this->siteConfig->get('default_langcode'));
}

if (preg_match($rule->getRegex(), $current_path) === 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function form(array $form, FormStateInterface $form_state): array {
'#type' => 'checkbox',
'#title' => $this->t('Match on path alias for site default language.'),
'#default_value' => $rule->matchOnSiteDefaultLanguage(),
'#description' => $this->t("If you select this checkbox, regexp would be applied for all aliases. E.g.: if you have /news/ regexp value, rule could be applied for /fr/nouvelles/* and /nl/nieuws/* pathes."),
'#description' => $this->t("If checked, the matching will be done on the path alias for the site default language. For example, if you have the /news/ regex value, the rule would be applied to the '/fr/nouvelles/*' as well as the /nl/nieuws/* paths."),
];

$form['regex'] = [
Expand Down

0 comments on commit ba92047

Please sign in to comment.