You are here

function media_library_views_post_render in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/media_library/media_library.module \media_library_views_post_render()
  2. 9 core/modules/media_library/media_library.module \media_library_views_post_render()

Implements hook_views_post_render().

File

core/modules/media_library/media_library.module, line 189
Contains hook implementations for the media_library module.

Code

function media_library_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
  if ($view
    ->id() === 'media_library') {
    $output['#attached']['library'][] = 'media_library/view';
    if (strpos($view->current_display, 'widget') === 0) {
      try {
        $query = MediaLibraryState::fromRequest($view
          ->getRequest())
          ->all();
      } catch (InvalidArgumentException $e) {

        // MediaLibraryState::fromRequest() will throw an exception if the view
        // is being previewed, since not all required query parameters will be
        // present. In a preview, however, this can be omitted since we're
        // merely previewing.
        // @todo Use the views API for checking for the preview mode when it
        //   lands. https://www.drupal.org/project/drupal/issues/3060855
        if (empty($view->preview) && empty($view->live_preview)) {
          throw $e;
        }
      }

      // If the current query contains any parameters we use to contextually
      // filter the view, ensure they persist across AJAX rebuilds.
      // The ajax_path is shared for all AJAX views on the page, but our query
      // parameters are prefixed and should not interfere with any other views.
      // @todo Rework or remove this in https://www.drupal.org/node/2983451
      if (!empty($query)) {
        $ajax_path =& $output['#attached']['drupalSettings']['views']['ajax_path'];
        $parsed_url = UrlHelper::parse($ajax_path);
        $query = array_merge($query, $parsed_url['query']);
        $ajax_path = $parsed_url['path'] . '?' . UrlHelper::buildQuery($query);
      }
    }
  }
}