PrintableEntityManager.php in Printer and PDF versions for Drupal 8+ 2.x
File
src/PrintableEntityManager.php
View source
<?php
namespace Drupal\printable;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityInterface;
class PrintableEntityManager implements PrintableEntityManagerInterface {
protected $entityTypeManager;
protected $configFactory;
protected $compatibleEntities = [];
public function __construct(EntityTypeManagerInterface $entity_type_manager, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->configFactory = $config_factory;
}
public function getEntityName(EntityInterface $entity) {
return $entity
->getEntityTypeId();
}
public function getPrintableEntities() {
$compatible_entities = $this
->getCompatibleEntities();
$entities = [];
foreach ($this->configFactory
->get('printable.settings')
->get('printable_entities') as $entity_type) {
if (isset($compatible_entities[$entity_type])) {
$entities[$entity_type] = $compatible_entities[$entity_type];
}
}
return $entities;
}
public function isPrintableEntity(EntityInterface $entity) {
return array_key_exists($entity
->getEntityTypeId(), $this
->getPrintableEntities());
}
public function getCompatibleEntities() {
if (empty($this->compatibleEntities)) {
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type => $entity_definition) {
if ($entity_definition
->hasHandlerClass('view_builder')) {
$this->compatibleEntities[$entity_type] = $entity_definition;
}
}
}
return $this->compatibleEntities;
}
}