You are here

class EventSubscriber in Opigno certificate 8

Same name and namespace in other branches
  1. 3.x src/EventSubscriber/EventSubscriber.php \Drupal\opigno_certificate\EventSubscriber\EventSubscriber

Class EventSubscriber.

Hierarchy

  • class \Drupal\opigno_certificate\EventSubscriber\EventSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EventSubscriber

1 string reference to 'EventSubscriber'
opigno_certificate.services.yml in ./opigno_certificate.services.yml
opigno_certificate.services.yml
1 service uses EventSubscriber
opigno_certificate.event_subscriber in ./opigno_certificate.services.yml
\Drupal\opigno_certificate\EventSubscriber\EventSubscriber

File

src/EventSubscriber/EventSubscriber.php, line 17

Namespace

Drupal\opigno_certificate\EventSubscriber
View source
class EventSubscriber implements EventSubscriberInterface {

  /**
   * Sets paper orientation depending to certificate paper_orientation field.
   */
  public function alterConfiguration(GenericEvent $event) {
    $route_name = \Drupal::routeMatch()
      ->getRouteName();
    if ($route_name == 'certificate.entity.pdf') {
      $route_params = \Drupal::routeMatch()
        ->getParameters();
      $type = $route_params
        ->get('entity_type');
      if ($type == 'group') {
        $id = $route_params
          ->get('entity_id');
        $group = Group::load($id);
        if ($group) {
          $certificate_id = $group
            ->get('field_certificate')->target_id;
          $certificate = OpignoCertificate::load($certificate_id);
          if ($certificate && $certificate
            ->hasField('paper_orientation')) {
            $orientation = $certificate
              ->get('paper_orientation')->value;
            $configuration = $event
              ->getArgument('configuration');
            $configuration['default_paper_orientation'] = $configuration['orientation'] = !empty($orientation) ? $orientation : 'portrait';
            $event
              ->setArgument('configuration', $configuration);
          }
        }
      }
    }
  }

  /**
   * Loads dompdf.
   */
  public function requireDompdf(GetResponseEvent $event) {
    $dompdf_autoloaders = [
      'libraries/dompdf/src/Autoloader.php',
      'profiles/opigno_lms/libraries/dompdf/src/Autoloader.php',
    ];
    foreach ($dompdf_autoloaders as $dompdf_autoloader) {
      if (file_exists($dompdf_autoloader)) {

        // Load dompdf for the entity_print.
        require_once $dompdf_autoloader;
        Autoloader::register();
        break;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[KernelEvents::REQUEST][] = [
      'requireDompdf',
    ];
    $events[PrintEvents::CONFIGURATION_ALTER] = [
      'alterConfiguration',
    ];
    return $events;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EventSubscriber::alterConfiguration public function Sets paper orientation depending to certificate paper_orientation field.
EventSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EventSubscriber::requireDompdf public function Loads dompdf.