Skip to content
Open
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
5 changes: 5 additions & 0 deletions domain/domain.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ services:
domain.loader:
class: Drupal\domain\DomainLoader
arguments: ['@config.typed', '@config.factory']
domain.twig_extension:
class: Drupal\domain\TwigExtension
arguments: ['@domain.negotiator']
tags:
- {name: twig.extension}
63 changes: 63 additions & 0 deletions domain/src/TwigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Drupal\domain;

/**
* Class TwigExtension
*
* @package Drupal\domain
*/
class TwigExtension extends \Twig_Extension {

/**
* @var \Drupal\domain\DomainNegotiatorInterface
*/
protected $domainNegotiator;

/**
* TwigExtension constructor.
*
* @param \Drupal\domain\DomainNegotiatorInterface $domainNegotiator
* Domain negotiator to work out the current domain.
*/
public function __construct(DomainNegotiatorInterface $domainNegotiator) {
$this->domainNegotiator = $domainNegotiator;
}

/**
* {@inheritdoc}
*/
public function getGlobals() {
if ($domain = $this->domainNegotiator->getActiveDomain()) {
return [
'domain_active' => $domain,
'domain_active_id' => $domain->id(),
];
}
else {
return [];
}
}

/**
* {@inheritdoc}
*/
public function getFunctions() {
return [
new \Twig_SimpleFunction('is_domain', [$this, 'isDomain']),
];
}

/**
* Check of the current domain id.
*
* @param $domain_id
* Id of the domain to check.
*
* @return bool
* if the id passed in a the current domain.
*/
public function isDomain($domain_id) {
return $this->domainNegotiator->getActiveId() == $domain_id;
}
}