diff --git a/domain/domain.services.yml b/domain/domain.services.yml index 8b87c6a5..c7f6460f 100644 --- a/domain/domain.services.yml +++ b/domain/domain.services.yml @@ -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} diff --git a/domain/src/TwigExtension.php b/domain/src/TwigExtension.php new file mode 100644 index 00000000..f0489a5d --- /dev/null +++ b/domain/src/TwigExtension.php @@ -0,0 +1,63 @@ +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; + } +}