You are here

class PrintableEntityManager in Printer and PDF versions for Drupal 8+ 8

Same name and namespace in other branches
  1. 2.x src/PrintableEntityManager.php \Drupal\printable\PrintableEntityManager

Helper class for the printable module.

Hierarchy

Expanded class hierarchy of PrintableEntityManager

1 file declares its use of PrintableEntityManager
PrintableEntityManagerTest.php in tests/src/Unit/PrintableEntityManagerTest.php
1 string reference to 'PrintableEntityManager'
printable.services.yml in ./printable.services.yml
printable.services.yml
1 service uses PrintableEntityManager
printable.entity_manager in ./printable.services.yml
Drupal\printable\PrintableEntityManager

File

src/PrintableEntityManager.php, line 13

Namespace

Drupal\printable
View source
class PrintableEntityManager implements PrintableEntityManagerInterface {

  /**
   * The entity manager service.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * The config factory service.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * The entity definitions of entities that have printable versions available.
   *
   * @var array
   */
  protected $compatibleEntities = [];

  /**
   * Constructs a new PrintableEntityManager object.
   *
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory service.
   */
  public function __construct(EntityManagerInterface $entity_manager, ConfigFactoryInterface $config_factory) {
    $this->entityManager = $entity_manager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityName(EntityInterface $entity) {
    return $entity
      ->getEntityTypeId();
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function isPrintableEntity(EntityInterface $entity) {
    return array_key_exists($entity
      ->getEntityTypeId(), $this
      ->getPrintableEntities());
  }

  /**
   * {@inheritdoc}
   */
  public function getCompatibleEntities() {

    // If the entities are yet to be populated, get the entity definitions from
    // the entity manager.
    if (empty($this->compatibleEntities)) {
      foreach ($this->entityManager
        ->getDefinitions() as $entity_type => $entity_definition) {

        // If this entity has a render controller, it has a printable version.
        if ($entity_definition
          ->hasHandlerClass('view_builder')) {
          $this->compatibleEntities[$entity_type] = $entity_definition;
        }
      }
    }
    return $this->compatibleEntities;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrintableEntityManager::$compatibleEntities protected property The entity definitions of entities that have printable versions available.
PrintableEntityManager::$configFactory protected property The config factory service.
PrintableEntityManager::$entityManager protected property The entity manager service.
PrintableEntityManager::getCompatibleEntities public function Get the entities that Printable can generate hardcopies for. Overrides PrintableEntityManagerInterface::getCompatibleEntities
PrintableEntityManager::getEntityName public function Gets the ID of the type of the entity. Overrides PrintableEntityManagerInterface::getEntityName
PrintableEntityManager::getPrintableEntities public function Get the entities that printable is available for. Overrides PrintableEntityManagerInterface::getPrintableEntities
PrintableEntityManager::isPrintableEntity public function Check if an entity has a printable version available for it. Overrides PrintableEntityManagerInterface::isPrintableEntity
PrintableEntityManager::__construct public function Constructs a new PrintableEntityManager object.