EntityPrintPluginManager.php in Entity Print 8
File
src/Plugin/EntityPrintPluginManager.php
View source
<?php
namespace Drupal\entity_print\Plugin;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\entity_print\Event\PdfEvents;
use Drupal\entity_print\PdfEngineException;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class EntityPrintPluginManager extends DefaultPluginManager {
protected $dispatcher;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EventDispatcherInterface $dispatcher) {
parent::__construct('Plugin/EntityPrint/PdfEngine', $namespaces, $module_handler, 'Drupal\\entity_print\\Plugin\\PdfEngineInterface', 'Drupal\\entity_print\\Annotation\\PdfEngine');
$this
->alterInfo('entity_print_pdf_engine');
$this
->setCacheBackend($cache_backend, 'entity_print_pdf_engines');
$this->dispatcher = $dispatcher;
}
public function createInstance($plugin_id, array $configuration = []) {
$configuration = array_merge($this
->getPdfEngineSettings($plugin_id), $configuration);
$definition = $this
->getDefinition($plugin_id);
$class = $definition['class'];
if (!$class::dependenciesAvailable()) {
throw new PdfEngineException(sprintf('Missing dependencies. %s', $class::getInstallationInstructions()));
}
return parent::createInstance($plugin_id, $configuration);
}
protected function getPdfEngineSettings($plugin_id) {
$storage = \Drupal::entityTypeManager()
->getStorage('pdf_engine');
if (!($entity = $storage
->load($plugin_id))) {
$entity = $storage
->create([
'id' => $plugin_id,
]);
}
$configuration = $entity
->getSettings();
$event = new GenericEvent(PdfEvents::CONFIGURATION_ALTER, [
'configuration' => $configuration,
'config' => $entity,
]);
$this->dispatcher
->dispatch(PdfEvents::CONFIGURATION_ALTER, $event);
$configuration = $event
->getArgument('configuration');
return $configuration;
}
}