FacetBlockAjaxController.php in Facets 8
File
src/Controller/FacetBlockAjaxController.php
View source
<?php
namespace Drupal\facets\Controller;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\InvokeCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Path\CurrentPathStack;
use Drupal\Core\PathProcessor\PathProcessorManager;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Routing\CurrentRouteMatch;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\RouterInterface;
class FacetBlockAjaxController extends ControllerBase {
protected $storage;
protected $renderer;
protected $currentPath;
protected $router;
protected $pathProcessor;
protected $currentRouteMatch;
public function __construct(RendererInterface $renderer, CurrentPathStack $currentPath, RouterInterface $router, PathProcessorManager $pathProcessor, CurrentRouteMatch $currentRouteMatch) {
$this->storage = $this
->entityTypeManager()
->getStorage('block');
$this->renderer = $renderer;
$this->currentPath = $currentPath;
$this->router = $router;
$this->pathProcessor = $pathProcessor;
$this->currentRouteMatch = $currentRouteMatch;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('renderer'), $container
->get('path.current'), $container
->get('router'), $container
->get('path_processor_manager'), $container
->get('current_route_match'));
}
public function ajaxFacetBlockView(Request $request) {
$response = new AjaxResponse();
$path = $request->request
->get('facet_link');
$facets_blocks = $request->request
->get('facets_blocks');
if (empty($path) || empty($facets_blocks)) {
throw new NotFoundHttpException('No facet link or facet blocks found.');
}
$facets_blocks = array_unique($facets_blocks);
$new_request = Request::create($path);
$request_stack = new RequestStack();
$processed = $this->pathProcessor
->processInbound($path, $new_request);
$this->currentPath
->setPath($processed);
$request->attributes
->add($this->router
->matchRequest($new_request));
$this->currentRouteMatch
->resetRouteMatch();
$request_stack
->push($new_request);
$container = \Drupal::getContainer();
$container
->set('request_stack', $request_stack);
$active_facet = $request->request
->get('active_facet');
foreach ($facets_blocks as $block_id => $block_selector) {
$block_entity = $this->storage
->load($block_id);
if ($block_entity) {
$block_view = $this->entityTypeManager
->getViewBuilder('block')
->view($block_entity);
$block_view = (string) $this->renderer
->renderPlain($block_view);
$response
->addCommand(new ReplaceCommand($block_selector, $block_view));
}
}
$response
->addCommand(new InvokeCommand('[data-block-plugin-id="' . $active_facet . '"]', 'addClass', [
'facet-active',
]));
$update_summary_block = $request->request
->get('update_summary_block');
if ($update_summary_block) {
$facet_summary_block_id = $request->request
->get('facet_summary_block_id');
$facet_summary_wrapper_id = $request->request
->get('facet_summary_wrapper_id');
$facet_summary_block_id = str_replace('-', '_', $facet_summary_block_id);
if ($facet_summary_block_id) {
$block_entity = $this->storage
->load($facet_summary_block_id);
$block_view = $this->entityTypeManager
->getViewBuilder('block')
->view($block_entity);
$block_view = (string) $this->renderer
->renderPlain($block_view);
$response
->addCommand(new ReplaceCommand('[data-drupal-facets-summary-id=' . $facet_summary_wrapper_id . ']', $block_view));
}
}
return $response;
}
}