You are here

public function AdvancedInsertViewController::ajaxView in Advanced Insert View 8

Same name and namespace in other branches
  1. 2.0.x src/Controller/AdvancedInsertViewController.php \Drupal\insert_view_adv\Controller\AdvancedInsertViewController::ajaxView()

Loads and renders a view via AJAX.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The current request object.

\Drupal\filter\Entity\FilterFormat|null $filter_format:

Return value

\Drupal\Core\Ajax\AjaxResponse The view response as ajax response.

1 string reference to 'AdvancedInsertViewController::ajaxView'
insert_view_adv.routing.yml in ./insert_view_adv.routing.yml
insert_view_adv.routing.yml

File

src/Controller/AdvancedInsertViewController.php, line 64

Class

AdvancedInsertViewController
Class AdvancedInsertViewController

Namespace

Drupal\insert_view_adv\Controller

Code

public function ajaxView(Request $request, FilterFormat $filter_format = NULL) {
  $name = $request->request
    ->get('view_name');
  $display_id = $request->request
    ->get('view_display_id');
  if (isset($name) && isset($display_id)) {
    $args = $request->request
      ->get('view_args');
    $args = isset($args) && $args !== '' ? explode('/', $args) : [];

    // Arguments can be empty, make sure they are passed on as NULL so that
    // argument validation is not triggered.
    $args = array_map(function ($arg) {
      return $arg == '' ? NULL : $arg;
    }, $args);
    if ($args) {

      // Transform the arguments back to string.
      $args = implode('/', $args);
    }
    $context = new RenderContext();
    $configuration = $filter_format
      ->filters('insert_view_adv')
      ->getConfiguration();
    $configuration = Json::encode($configuration);
    $preview = $this->renderer
      ->executeInRenderContext($context, function () use ($name, $display_id, $args, $configuration) {
      return InsertView::build($name, $display_id, $args, $configuration);
    });
    if (!$context
      ->isEmpty() && !empty($preview)) {
      $bubbleable_metadata = $context
        ->pop();
      BubbleableMetadata::createFromRenderArray($preview)
        ->merge($bubbleable_metadata)
        ->applyTo($preview);
    }
    $response = new AjaxResponse();
    $response
      ->addCommand(new InsertViewCommand($preview));
    return $response;
  }
  else {
    throw new NotFoundHttpException();
  }
}