drupalorg-1.0.x-dev/src/Controller/ViewOutputController.php

src/Controller/ViewOutputController.php
<?php

namespace Drupal\drupalorg\Controller;

use Drupal\Core\Cache\CacheableResponse;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\Renderer;
use Drupal\Core\Site\Settings;
use Drupal\views\Entity\View;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Returns output of Views for embedding in the legacy site.
 */
class ViewOutputController extends ControllerBase {

  /**
   * {@inheritdoc}
   */
  public function __construct(
    #[Autowire(service: 'renderer')]
    protected Renderer $renderer,
  ) {
  }

  /**
   * Returns HTML output of a View display.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param \Drupal\views\Entity\View $view
   *   The view entity.
   * @param string $display_id
   *   The display ID.
   *
   * @return \Drupal\Core\Cache\CacheableResponse
   *   The rendered HTML output.
   */
  public function viewOutput(Request $request, View $view, string $display_id): CacheableResponse {
    if (!in_array($view->id() . ':' . $display_id, Settings::get('drupalorg_allowed_view_output_displays', []))) {
      throw new NotFoundHttpException();
    }
    if (empty($view->getDisplay($display_id))) {
      throw new NotFoundHttpException();
    }
    $output = views_embed_view($view->id(), $display_id, ...$request->query->all('args'));
    $output['#cache']['contexts'][] = 'url.query_args';
    $response = new CacheableResponse($this->renderer->render($output));
    $response->addCacheableDependency($view);
    $response->getCacheableMetadata()->addCacheTags(['args:' . implode(':', $request->query->all('args'))]);
    return $response;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc