-
-
Save LionsAd/0dd827b1b3c719edc19e039cdc807f7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* | |
* Contains Drupal\newsletter_view\Controller\NewsletterViewNodeController | |
*/ | |
namespace Drupal\newsletter_view\Controller; | |
use Drupal\Core\Access\AccessResult; | |
use Drupal\Core\Controller\TitleResolverInterface; | |
use Drupal\Core\Extension\ModuleHandlerInterface; | |
use Drupal\Core\Render\BubbleableMetadata; | |
use Drupal\Core\Render\HtmlResponse; | |
use Drupal\Core\Render\RenderCacheInterface; | |
use Drupal\Core\Render\RenderContext; | |
use Drupal\Core\Routing\RouteMatchInterface; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\node\Controller\NodeViewController; | |
use Drupal\Core\Entity\EntityInterface; | |
use Symfony\Component\HttpFoundation\Response; | |
use Pelago\Emogrifier; | |
use Symfony\Component\DependencyInjection\ContainerInterface; | |
use Drupal\Core\Entity\EntityManagerInterface; | |
use Drupal\Core\Render\RendererInterface; | |
class NewsletterViewNodeController extends NodeViewController { | |
protected $emogrifier; | |
protected $titleResolver; | |
protected $routeMatch; | |
protected $moduleHandler; | |
protected $renderCache; | |
public function __construct(EntityManagerInterface $entity_manager, RendererInterface $renderer, Emogrifier $emogrifier, TitleResolverInterface $title_resolver, RouteMatchInterface $route_match, ModuleHandlerInterface $module_handler, RenderCacheInterface $render_cache) { | |
parent::__construct($entity_manager, $renderer); | |
$this->emogrifier = $emogrifier; | |
$this->titleResolver = $title_resolver; | |
$this->routeMatch = $route_match; | |
$this->moduleHandler = $module_handler; | |
$this->renderCache = $render_cache; | |
} | |
/** | |
* {@inheritdoc} | |
*/ | |
public static function create(ContainerInterface $container) { | |
return new static( | |
$container->get('entity.manager'), | |
$container->get('renderer'), | |
$container->get('emogrifier'), | |
$container->get('title_resolver'), | |
$container->get('current_route_match'), | |
$container->get('module_handler'), | |
$container->get('render_cache') | |
); | |
} | |
public function view(EntityInterface $node, $view_mode = 'full', $langcode = NULL) { | |
$render_array = array(); | |
$render_context = new RenderContext(); | |
$this->renderer->executeInRenderContext($render_context, function() use (&$render_array, $node, $view_mode, $langcode) { | |
$render_array = $this->viewHelper($node, $view_mode, $langcode); | |
}); | |
$bubbleable_metadata = $render_context->pop(); | |
$bubbleable_metadata->applyTo($render_array); | |
$content = $this->renderCache->getCacheableRenderArray($render_array); | |
$content['#cache']['tags'][] = 'rendered'; | |
$response = new HtmlResponse($content, 200, [ | |
'Content-Type' => 'text/html; charset=UTF-8', | |
]); | |
return $response; | |
} | |
protected function viewHelper(EntityInterface $node, $view_mode = 'full', $langcode = NULL) | |
$page = $this->entityManager | |
->getViewBuilder($node->getEntityTypeId()) | |
->view($node, $view_mode); | |
$page['#pre_render'][] = [$this, 'buildTitle']; | |
$page['#entity_type'] = $node->getEntityTypeId(); | |
$page['#' . $page['#entity_type']] = $node; | |
$render_array = [ | |
'#theme' => 'newsletter', | |
'#title' => $this->titleResolver->getTitle(\Drupal::request(), $this->routeMatch->getRouteObject()), | |
'#content' => $page, | |
]; | |
$this->renderer->render($render_array); | |
$content = $this->renderCache->getCacheableRenderArray($render_array); | |
$path = drupal_get_path('theme', 'newsletter') . '/css/newsletter.css'; | |
if (file_exists($path)) { | |
$this->emogrifier->setCss(file_get_contents($path)); | |
} | |
$this->emogrifier->enableCssToHtmlMapping(); | |
$this->emogrifier->setHtml($content['#markup']); | |
$content['#markup'] = preg_replace('/^\s+|\n|\r|\s+$/m', '', $this->emogrifier->emogrify()); | |
$this->moduleHandler->alter('emogrify_html', $content['#markup']); | |
return $content; | |
} | |
public function access(AccountInterface $account, EntityInterface $node) { | |
return AccessResult::allowedIf($node->isPublished() || $account->hasPermission('bypass node access')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment