iframe_consent-1.0.x-dev/src/Controller/AjaxController.php

src/Controller/AjaxController.php
<?php

namespace Drupal\iframe_consent\Controller;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Controller for AJAX callbacks.
 */
class AjaxController extends ControllerBase {

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * The iframe consent settings service.
   *
   * @var \Drupal\iframe_consent\Service\IframeConsentHelper
   */
  protected $iframeConsentHelper;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    $instance = parent::create($container);
    $instance->renderer = $container->get('renderer');
    $instance->iframeConsentHelper = $container->get('iframe_consent.helper');
    return $instance;
  }

  /**
   * AJAX callback to return iframe placeholder content.
   *
   * @param string $id
   *   The ID of the placeholder.
   * @param string $hash
   *   The hash to identify the placeholder.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   The AJAX response.
   */
  public function ajaxCallback(string $id, string $hash) {
    $response = new AjaxResponse();

    if (!$this->iframeConsentHelper->settings->isEnabled()) {
      return $response->setStatusCode(400);
    }

    $template_id = $this->validateTemplateId($id);

    // Build the content based on the ID.
    $build = [
      '#theme' => 'iframe_consent_placeholder__' . $template_id,
      '#manage_button_label' => $this->iframeConsentHelper->settings->get('manage_button_label'),
      '#accept_button_label' => $this->iframeConsentHelper->settings->get('accept_button_label'),
      '#display_accept_button' => $this->iframeConsentHelper->settings->get('display_accept_button') ? TRUE : FALSE,
      '#body' => [
        '#type' => 'processed_text',
        '#text' => $this->iframeConsentHelper->settings->get('body')['value'],
        '#format' => $this->iframeConsentHelper->settings->get('body')['format'],
      ],
    ];

    $this->iframeConsentHelper->setCacheTags($build);

    $html = $this->renderer->render($build);
    $response->addCommand(new HtmlCommand('#iframe-consent-placeholder--' . $hash, $html));

    return $response;
  }

  /**
   * Validates the template ID.
   *
   * @param string $id
   *   The ID to validate.
   *
   * @return string
   *   A validated template ID.
   */
  protected function validateTemplateId(string $id): string {
    if (!$this->iframeConsentHelper->settings->get('templates_by_domain')) {
      return 'default';
    }

    $domain = $this->iframeConsentHelper->getDomainByTemplateId($id);
    if (empty($domains)) {
      return 'default';
    }

    if ($this->iframeConsentHelper->isInDomainsList($domain)) {
      return $id;
    }

    return 'default';
  }

}

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

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