public function FacetBlockAjaxController::ajaxFacetBlockView in Facets 8
Loads and renders the facet blocks via AJAX.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object.
Return value
\Drupal\Core\Ajax\AjaxResponse The ajax response.
Throws
\Symfony\Component\HttpKernel\Exception\NotFoundHttpException Thrown when the view was not found.
1 string reference to 'FacetBlockAjaxController::ajaxFacetBlockView'
File
- src/
Controller/ FacetBlockAjaxController.php, line 114
Class
- FacetBlockAjaxController
- Defines a controller to load a facet via AJAX.
Namespace
Drupal\facets\ControllerCode
public function ajaxFacetBlockView(Request $request) {
$response = new AjaxResponse();
// Rebuild the request and the current path, needed for facets.
$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.');
}
// Make sure we are not updating blocks multiple times.
$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');
// Build the facets blocks found for the current request and update.
foreach ($facets_blocks as $block_id => $block_selector) {
$block_entity = $this->storage
->load($block_id);
if ($block_entity) {
// Render a block, then add it to the response as a replace command.
$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 filter summary block.
$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;
}