From 2e8db2ff104b43fa36363cf6f0b597ce02a41e39 Mon Sep 17 00:00:00 2001 From: Gordon Heydon Date: Tue, 5 Mar 2019 15:20:46 +1100 Subject: [PATCH 1/3] Create some handy twig global variables and functions to assist with theming. --- domain/domain.services.yml | 5 ++++ domain/src/TwigExtension.php | 56 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 domain/src/TwigExtension.php 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..d655fed1 --- /dev/null +++ b/domain/src/TwigExtension.php @@ -0,0 +1,56 @@ +domainNegotiator = $domainNegotiator; + } + + /** + * {@inheritdoc} + */ + public function getGlobals() { + return [ + 'domain_active' => $this->domainNegotiator->getActiveDomain(), + 'domain_active_id' => $this->domainNegotiator->getActiveId(), + ]; + } + + /** + * {@inheritdoc} + */ + public function getFunctions() { + 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; + } +} \ No newline at end of file From ca85655b4f401d458d66a7af14ce8d9c760e1b90 Mon Sep 17 00:00:00 2001 From: Gordon Heydon Date: Tue, 12 Mar 2019 23:52:45 +1100 Subject: [PATCH 2/3] Fix up issue when there are no domains configured. --- domain/src/TwigExtension.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/domain/src/TwigExtension.php b/domain/src/TwigExtension.php index d655fed1..7d5b48c9 100644 --- a/domain/src/TwigExtension.php +++ b/domain/src/TwigExtension.php @@ -28,10 +28,15 @@ public function __construct(DomainNegotiatorInterface $domainNegotiator) { * {@inheritdoc} */ public function getGlobals() { - return [ - 'domain_active' => $this->domainNegotiator->getActiveDomain(), - 'domain_active_id' => $this->domainNegotiator->getActiveId(), - ]; + if ($domain = $this->domainNegotiator->getActiveDomain()) { + return [ + 'domain_active' => $domain, + 'domain_active_id' => $domain->id(), + ]; + } + else { + return []; + } } /** From 48dc11709e64c56575078705938765ea35d49ea2 Mon Sep 17 00:00:00 2001 From: Gordon Heydon Date: Thu, 12 Sep 2019 15:17:00 +1000 Subject: [PATCH 3/3] Fix up the return from ::getFunctions() to be an array. --- domain/src/TwigExtension.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/domain/src/TwigExtension.php b/domain/src/TwigExtension.php index 7d5b48c9..f0489a5d 100644 --- a/domain/src/TwigExtension.php +++ b/domain/src/TwigExtension.php @@ -43,7 +43,9 @@ public function getGlobals() { * {@inheritdoc} */ public function getFunctions() { - new \Twig_SimpleFunction('is_domain', [$this, 'isDomain']); + return [ + new \Twig_SimpleFunction('is_domain', [$this, 'isDomain']), + ]; } /** @@ -58,4 +60,4 @@ public function getFunctions() { public function isDomain($domain_id) { return $this->domainNegotiator->getActiveId() == $domain_id; } -} \ No newline at end of file +}