AssetCollector.php in Entity Print 8.2
File
src/Asset/AssetCollector.php
View source
<?php
namespace Drupal\entity_print\Asset;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\InfoParserInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\entity_print\Event\PrintCssAlterEvent;
use Drupal\entity_print\Event\PrintEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class AssetCollector implements AssetCollectorInterface {
protected $themeHandler;
protected $infoParser;
protected $dispatcher;
public function __construct(ThemeHandlerInterface $theme_handler, InfoParserInterface $info_parser, EventDispatcherInterface $event_dispatcher) {
$this->themeHandler = $theme_handler;
$this->infoParser = $info_parser;
$this->dispatcher = $event_dispatcher;
}
public function getCssLibraries(array $entities) {
$libraries = [];
$theme = $this->themeHandler
->getTheme($this->themeHandler
->getDefault());
$theme_info = $this->infoParser
->parse($theme
->getPathname());
if (isset($theme_info['entity_print'])) {
if (isset($theme_info['entity_print']['all'])) {
$libraries = array_merge($libraries, (array) $theme_info['entity_print']['all']);
unset($theme_info['entity_print']['all']);
}
foreach ($entities as $entity) {
$this
->buildCssForEntity($entity, $theme_info['entity_print'], $libraries);
}
}
$this->dispatcher
->dispatch(PrintEvents::CSS_ALTER, new PrintCssAlterEvent($libraries, $entities));
return $libraries;
}
protected function buildCssForEntity(EntityInterface $entity, array $theme_info, array &$libraries) {
foreach ($theme_info as $key => $value) {
if ($key !== $entity
->getEntityTypeId()) {
continue;
}
foreach ($value as $css_bundle => $css) {
if ($css_bundle === 'all' || $entity
->bundle() === $css_bundle) {
$libraries = array_merge($libraries, (array) $css);
}
}
}
}
}