materio-d9/web/modules/custom/materio_decoupled/src/Controller/AjaxHeaderMenuBlock.php

55 lines
1.5 KiB
PHP

<?php
namespace Drupal\materio_decoupled\Controller;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\block\Entity\Block;
use Symfony\Component\HttpFoundation\JsonResponse;
// use Drupal\Core\Cache\CacheableJsonResponse;
// use Drupal\Core\Cache\CacheableMetadata;
// use Drupal\core\render\RenderContext;
/**
* Defines a route controller.
*/
class AjaxHeaderMenuBlock extends ControllerBase {
private function getBlockDefinition(){
// $language = \Drupal::languageManager()->getCurrentLanguage()->getId();
// \Drupal::logger('materio_user')->notice($language);
$this->bid = "header";
$this->block = Block::load($this->bid);
$this->block_builded = \Drupal::entityManager()->getViewBuilder('block')->view($this->block);
}
/**
* Handler for getBlock request.
*/
public function getBlock(Request $request) {
$this->getBlockDefinition();
$rendered = \Drupal::service('renderer')->renderRoot($this->block_builded);
$data = [
'rendered' => $rendered,
// '#cache' => [
// 'max-age' => \Drupal\Core\Cache\Cache::PERMANENT,
// 'tags' => [
// 'materio_sapi-search_form-cache',
// ]
// ]
];
$response = new JsonResponse();
$response->setData($data);
// $response = new CacheableJsonResponse($data);
// $response->addCacheableDependency(CacheableMetadata::createFromRenderArray($data));
return $response;
}
}