Skip to content

Instantly share code, notes, and snippets.

@webflo
Created September 27, 2024 12:47
Show Gist options
  • Save webflo/67a380250486f8ab2ccacc73efe21406 to your computer and use it in GitHub Desktop.
Save webflo/67a380250486f8ab2ccacc73efe21406 to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\foo;
use Drupal\Core\Asset\LibraryDiscoveryCollector;
use Drupal\Core\Session\AccountInterface;
use Drupal\domain\DomainNegotiatorInterface;
/**
* LibraryDiscoveryCollector with domain and permissions context.
*
* Fixes https://www.drupal.org/project/gin_toolbar/issues/3293209 and bugs with
* dialog css in frontend theme.
*/
class ContextAwareLibraryDiscoveryCollector extends LibraryDiscoveryCollector {
protected DomainNegotiatorInterface $domainNegotiator;
protected AccountInterface $currentUser;
public function setDomainNegotiator(DomainNegotiatorInterface $domainNegotiator) {
$this->domainNegotiator = $domainNegotiator;
}
public function setCurrentUser(AccountInterface $currentUser) {
$this->currentUser = $currentUser;
}
protected function getCid() {
if (!isset($this->cid)) {
$cid = [
'library_info',
$this->domainNegotiator->getActiveId(),
// Fix for https://www.drupal.org/project/gin_toolbar/issues/3293209
(int) $this->currentUser->hasPermission('access toolbar'),
$this->themeManager->getActiveTheme()->getName()
];
$this->cid = implode(':', $cid);
}
return $this->cid;
}
}
<?php
namespace Drupal\foo;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceProviderBase;
use Symfony\Component\DependencyInjection\Reference;
/**
* Defines a service provider for the Demeter Admin module.
*/
class FooServiceProvider extends ServiceProviderBase {
/**
* {@inheritdoc}
*/
public function alter(ContainerBuilder $container) {
$container->removeDefinition('domain_config.library.discovery.collector');
$definition = $container->getDefinition('library.discovery.collector');
$definition->setClass(ContextAwareLibraryDiscoveryCollector::class);
$definition->addMethodCall('setDomainNegotiator', [new Reference('domain.negotiator')]);
$definition->addMethodCall('setCurrentUser', [new Reference('current_user')]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment