ThunderGqlsRenderer.php in Thunder 6.2.x
File
modules/thunder_gqls/src/Render/MainContent/ThunderGqlsRenderer.php
View source
<?php
namespace Drupal\thunder_gqls\Render\MainContent;
use Drupal\Core\Breadcrumb\BreadcrumbBuilderInterface;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Render\MainContent\MainContentRendererInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\metatag\MetatagManager;
use Drupal\schema_metatag\SchemaMetatagManager;
use Symfony\Component\HttpFoundation\Request;
class ThunderGqlsRenderer implements MainContentRendererInterface {
protected $renderer;
protected $moduleHandler;
protected $breadcrumbManager;
protected $currentRouteMatch;
protected $metatagManager;
protected $languageManager;
public function __construct(ModuleHandlerInterface $moduleHandler, RendererInterface $renderer, BreadcrumbBuilderInterface $breadcrumbManager, CurrentRouteMatch $currentRouteMatch, MetatagManager $metatagManager, LanguageManagerInterface $languageManager) {
$this->renderer = $renderer;
$this->metatagManager = $metatagManager;
$this->breadcrumbManager = $breadcrumbManager;
$this->currentRouteMatch = $currentRouteMatch;
$this->moduleHandler = $moduleHandler;
$this->languageManager = $languageManager;
}
public function renderResponse(array $main_content, Request $request, RouteMatchInterface $route_match) {
$json = [];
$json['breadcrumb'] = $this
->breadcrumb();
$json['jsonld'] = $this
->jsonld();
$json['language'] = $this->languageManager
->getCurrentLanguage()
->getId();
$response = new CacheableJsonResponse($json, 200);
$response
->addCacheableDependency(CacheableMetadata::createFromRenderArray($main_content));
return $response;
}
protected function breadcrumb() : array {
$breadCrumb = [];
foreach ($this->breadcrumbManager
->build($this->currentRouteMatch
->getCurrentRouteMatch())
->getLinks() as $link) {
$breadCrumb[] = [
'uri' => $link
->getUrl()
->toUriString(),
'title' => $link
->getText(),
];
}
return $breadCrumb;
}
protected function jsonld() : string {
if (!$this->moduleHandler
->moduleExists('schema_metatag')) {
return '';
}
$entity = metatag_get_route_entity();
if (!$entity instanceof ContentEntityInterface) {
return '';
}
$metatags = [];
foreach ($this->metatagManager
->tagsFromEntityWithDefaults($entity) as $tag => $data) {
$metatags[$tag] = $data;
}
$context = [
'entity' => $entity,
];
$this->moduleHandler
->alter('metatags', $metatags, $context);
$elements = $this->metatagManager
->generateElements($metatags, $entity);
$context = new RenderContext();
$jsonldString = $this->renderer
->executeInRenderContext($context, function () use ($elements) {
if ($items = SchemaMetatagManager::parseJsonld($elements['#attached']['html_head'])) {
if ($jsonld = SchemaMetatagManager::encodeJsonld($items)) {
$html = SchemaMetatagManager::renderArrayJsonLd($jsonld);
return $this->renderer
->render($html);
}
}
});
return $jsonldString ?? '';
}
}