You are here

class PostRenderSubscriber in Entity Print 8

Same name and namespace in other branches
  1. 8.2 src/EventSubscriber/PostRenderSubscriber.php \Drupal\entity_print\EventSubscriber\PostRenderSubscriber

The PostRenderSubscriber class.

Hierarchy

  • class \Drupal\entity_print\EventSubscriber\PostRenderSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of PostRenderSubscriber

1 string reference to 'PostRenderSubscriber'
entity_print.services.yml in ./entity_print.services.yml
entity_print.services.yml
1 service uses PostRenderSubscriber
entity_print.post_render_subscriber in ./entity_print.services.yml
Drupal\entity_print\EventSubscriber\PostRenderSubscriber

File

src/EventSubscriber/PostRenderSubscriber.php, line 16

Namespace

Drupal\entity_print\EventSubscriber
View source
class PostRenderSubscriber implements EventSubscriberInterface {

  /**
   * The Config Factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * PostRenderSubscriber constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(ConfigFactoryInterface $config_factory, RequestStack $request_stack) {
    $this->configFactory = $config_factory;
    $this->requestStack = $request_stack;
  }

  /**
   * Alter the HTML after it has been rendered.
   *
   * This is a temporary workaround for a core issue.
   * @see https://drupal.org/node/1494670
   *
   * @param \Drupal\entity_print\Event\PdfHtmlAlterEvent $event
   *   The event object.
   */
  public function postRender(PdfHtmlAlterEvent $event) {

    // We only apply the fix to PHP Wkhtmltopdf because the other implementations
    // allow us to specify a base url.
    if ($this->configFactory
      ->get('entity_print.settings')
      ->get('pdf_engine') !== 'phpwkhtmltopdf') {
      return;
    }
    $html_string =& $event
      ->getHtml();
    $html5 = new HTML5();
    $document = $html5
      ->loadHTML($html_string);

    // Define a function that will convert root relative uris into absolute urls.
    $transform = function ($tag, $attribute) use ($document) {
      $base_url = $this->requestStack
        ->getCurrentRequest()
        ->getSchemeAndHttpHost();
      foreach ($document
        ->getElementsByTagName($tag) as $node) {
        $attribute_value = $node
          ->getAttribute($attribute);

        // Handle protocol agnostic URLs as well.
        if (Unicode::substr($attribute_value, 0, 2) === '//') {
          $node
            ->setAttribute($attribute, $base_url . Unicode::substr($attribute_value, 1));
        }
        elseif (Unicode::substr($attribute_value, 0, 1) === '/') {
          $node
            ->setAttribute($attribute, $base_url . $attribute_value);
        }
      }
    };

    // Transform stylesheets, links and images.
    $transform('link', 'href');
    $transform('a', 'href');
    $transform('img', 'src');

    // Overwrite the HTML.
    $html_string = $html5
      ->saveHTML($document);
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      PdfEvents::POST_RENDER => 'postRender',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PostRenderSubscriber::$configFactory protected property The Config Factory.
PostRenderSubscriber::$requestStack protected property The request stack.
PostRenderSubscriber::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
PostRenderSubscriber::postRender public function Alter the HTML after it has been rendered.
PostRenderSubscriber::__construct public function PostRenderSubscriber constructor.