domain_views_display-1.x-dev/src/Routing/ControllerDecorator.php

src/Routing/ControllerDecorator.php
<?php

declare(strict_types=1);

namespace Drupal\domain_views_display\Routing;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\CacheableResponseInterface;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\DependencyInjection\ClassResolverInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\domain_views_display\DomainViewsDisplay;
use Drupal\views\Routing\ViewPageController;
use Drupal\views\Views;
use Symfony\Component\HttpFoundation\Response;

/**
 * Decorates ViewPageController to switch display ID.
 *
 * @see \Drupal\views\Routing\ViewPageController
 */
class ControllerDecorator implements ContainerInjectionInterface {

  use AutowireTrait;

  /**
   * The class being decorated.
   */
  public const string DECORATED_CLASS = ViewPageController::class;

  /**
   * Creates the decorator.
   *
   * @param \Drupal\Core\DependencyInjection\ClassResolverInterface $classResolver
   *   The class resolver service.
   */
  public function __construct(
    protected readonly ClassResolverInterface $classResolver,
  ) {}

  /**
   * Handles a response for a given view and display.
   *
   * @param string $view_id
   *   The ID of the view.
   * @param string $display_id
   *   The ID of the display.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The route match.
   *
   * @return array<int|string, mixed>|\Symfony\Component\HttpFoundation\Response
   *   A render array or a Response object.
   */
  public function handle($view_id, $display_id, RouteMatchInterface $route_match): array|Response {
    $view = Views::getView($view_id);
    assert($view !== NULL);
    $view->setDisplay($display_id);
    $cacheability = new CacheableMetadata();
    $override = $this->getDomainViewsDisplay()->getActiveOverride($view, $cacheability);
    $response = $this->getViewPageController()->handle($view_id, $override ?? $display_id, $route_match);
    if ($response instanceof CacheableResponseInterface) {
      $response->addCacheableDependency($cacheability);
    }
    return $response;
  }

  /**
   * Gets the title of the given view's display.
   *
   * @param string $view_id
   *   The id of the view.
   * @param string $display_id
   *   The id of the display from the view.
   *
   * @return string|\Drupal\Component\Render\MarkupInterface
   *   The title of the display of the view.
   */
  public function getTitle($view_id, $display_id = 'default') {
    $view = Views::getView($view_id);
    assert($view !== NULL);
    $view->setDisplay($display_id);
    $override = $this->getDomainViewsDisplay()->getActiveOverride($view);
    return $this->getViewPageController()
      ->getTitle($view_id, $override ?? $display_id);
  }

  /**
   * Gets the view page controller.
   */
  protected function getViewPageController(): ViewPageController {
    $controller = $this->classResolver->getInstanceFromDefinition(ViewPageController::class);
    assert($controller instanceof ViewPageController);
    return $controller;
  }

  /**
   * Gets the domain views display object.
   */
  protected function getDomainViewsDisplay(): DomainViewsDisplay {
    $domain_views_display = $this->classResolver->getInstanceFromDefinition(DomainViewsDisplay::class);
    assert($domain_views_display instanceof DomainViewsDisplay);
    return $domain_views_display;
  }

}

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

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