sites_simple_sitemap-1.0.0-alpha4/src/Plugin/simple_sitemap/UrlGenerator/SitesSimpleSitemapEntityUrlGenerator.php

src/Plugin/simple_sitemap/UrlGenerator/SitesSimpleSitemapEntityUrlGenerator.php
<?php

namespace Drupal\sites_simple_sitemap\Plugin\simple_sitemap\UrlGenerator;

use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Url;
use Drupal\path_alias\AliasManager;
use Drupal\simple_sitemap\Entity\EntityHelper;
use Drupal\simple_sitemap\Entity\SimpleSitemapInterface;
use Drupal\simple_sitemap\Logger;
use Drupal\simple_sitemap\Manager\EntityManager;
use Drupal\simple_sitemap\Plugin\simple_sitemap\SimpleSitemapPluginBase;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\EntityUrlGenerator;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorInterface;
use Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager;
use Drupal\simple_sitemap\Settings;
use Drupal\sites\Plugin\Site\SiteInterface;
use Drupal\sites\SitePluginManager;
use Drupal\sites\SitesUrlServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Matcher\RequestMatcherInterface;

/**
 * Provides the entity URL generator for the sites_simple_sitemap module.
 *
 * @UrlGenerator(
 *   id = "sites_simple_sitemap_entity",
 *   label = @Translation("Sites Entity URL generator"),
 *   description = @Translation("Generates URLs for entity bundles and bundle overrides for selected sites."),
 * )
 */
class SitesSimpleSitemapEntityUrlGenerator extends EntityUrlGenerator {

  /**
   * Site.
   *
   * @var \Drupal\sites\SiteInterface
   */
  protected $site = NULL;

  /**
   * SitesSimpleSitemapEntityUrlGenerator constructor.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\simple_sitemap\Logger $logger
   *   Simple XML Sitemap logger.
   * @param \Drupal\simple_sitemap\Settings $settings
   *   The simple_sitemap.settings service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\simple_sitemap\Entity\EntityHelper $entity_helper
   *   Helper class for working with entities.
   * @param \Drupal\simple_sitemap\Manager\EntityManager $entities_manager
   *   The simple_sitemap.entity_manager service.
   * @param \Drupal\simple_sitemap\Plugin\simple_sitemap\UrlGenerator\UrlGeneratorManager $url_generator_manager
   *   The UrlGenerator plugins manager.
   * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $memory_cache
   *   The memory cache.
   * @param \Drupal\Core\Access\AccessManagerInterface $accessManager
   *   The access check service.
   * @param \Symfony\Component\Routing\Matcher\RequestMatcherInterface $router
   *   The dynamic router service.
   * @param \Drupal\sites\SitePluginManager $siteManager
   *   The site manager.
   * @param \Drupal\sites\SitesUrlServiceInterface $sitesUrlService
   *   The sites url service.
   * @param \Drupal\path_alias\AliasManager $aliasManager
   *   The alias manager service.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    Logger $logger,
    Settings $settings,
    LanguageManagerInterface $language_manager,
    EntityTypeManagerInterface $entity_type_manager,
    EntityHelper $entity_helper,
    EntityManager $entities_manager,
    UrlGeneratorManager $url_generator_manager,
    MemoryCacheInterface $memory_cache,
    protected readonly AccessManagerInterface $accessManager,
    protected readonly RequestMatcherInterface $router,
    protected readonly SitePluginManager $siteManager,
    protected readonly SitesUrlServiceInterface $sitesUrlService,
    private readonly AliasManager $aliasManager,
  ) {
    parent::__construct(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $logger,
      $settings,
      $language_manager,
      $entity_type_manager,
      $entity_helper,
      $entities_manager,
      $url_generator_manager,
      $memory_cache
    );
  }

  /**
   * {@inheritdoc}
   */
  public static function create(
    ContainerInterface $container,
    array $configuration,
    $plugin_id,
    $plugin_definition,
  ): SimpleSitemapPluginBase {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('simple_sitemap.logger'),
      $container->get('simple_sitemap.settings'),
      $container->get('language_manager'),
      $container->get('entity_type.manager'),
      $container->get('simple_sitemap.entity_helper'),
      $container->get('simple_sitemap.entity_manager'),
      $container->get('plugin.manager.simple_sitemap.url_generator'),
      $container->get('entity.memory_cache'),
      $container->get('access_manager'),
      $container->get('router.no_access_checks'),
      $container->get('plugin.manager.site'),
      $container->get('sites.url.service'),
      $container->get('path_alias.manager'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function setSitemap(SimpleSitemapInterface $sitemap): UrlGeneratorInterface {
    $cloned_sitemap = clone $sitemap;
    $cloned_sitemap->set('id', $sitemap->getOriginalId());
    $sitemap_id = $sitemap->id();
    if (str_contains($sitemap_id, ':')) {
      $site_id = substr($sitemap_id, strpos($sitemap_id, ":") + 1);
      $this->site = $this->siteManager->createInstance($site_id);
    }

    return parent::setSitemap($cloned_sitemap);
  }

  /**
   * {@inheritdoc}
   */
  protected function replaceBaseUrlWithCustom(string $url): string {
    // If the sitemap contains a site ID, replace the base URL hostname with
    // the site hostname.
    if ($this->site &&
      ($hostname = $this->site->getSetting('hostnames')[0]) &&
      ($global_hostname = parse_url($GLOBALS['base_url'])['host'])
    ) {
      return str_replace($global_hostname, $hostname, $url);
    }

    // Otherwise, use the parent method from the simple_sitemap module.
    return parent::replaceBaseUrlWithCustom($url);
  }

  /**
   * {@inheritdoc}
   */
  protected function getUrlVariants(array $path_data, Url $url_object): array {
    $variants = parent::getUrlVariants($path_data, $url_object);
    foreach ($variants as $delta => $variant) {
      unset($variants[$delta]);
      if (!$this->site || $delta > 0) {
        continue;
      }
      $sites = $this->sitesUrlService->getSitesFromUrl($url_object, $this->site);
      foreach ($sites as $site) {
        $new_variant = [];

        if (isset($variant['alternate_urls'])) {
          foreach ($variant['alternate_urls'] as $alt_url_langcode => $alternative_url) {
            $new_variant['alternate_urls'][$alt_url_langcode] = $this->createFinalUrl($url_object, $site, $alt_url_langcode);
          }
        }
        $new_variant['url'] = $this->createFinalUrl($url_object, $site, $alt_url_langcode);
        $variants[] = $new_variant;
      }
    }
    return $variants;
  }

  /**
   * Creates a final URL with the appropriate language and hostname settings.
   *
   * This method clones a given URL object, sets the language option, and makes
   * the URL absolute. It then constructs the final URL using the hostname
   * settings from the site and the alias path for the specified language.
   *
   * @param \Drupal\Core\Url $urlObj
   *   The URL object to be processed.
   * @param \Drupal\site_api\Entity\SiteInterface $site
   *   The site entity containing hostname settings.
   * @param string $langCode
   *   The language code for which the URL is being generated.
   *
   * @return string
   *   The fully constructed URL as a string.
   */
  private function createFinalUrl(Url $urlObj, SiteInterface $site, string $langCode): string {
    $cloned_url = clone $urlObj;
    $cloned_url->setOption('language', $this->languages[$langCode]);
    $cloned_url->setAbsolute();
    $url_string = $cloned_url->toString();
    $url_parts = parse_url($url_string);

    $hostnames_setting = $site->getSetting('hostnames');
    if (count($hostnames_setting) > 0) {
      $hostname = $hostnames_setting['0'];
    }
    else {
      $hostname = $site->getSetting('hostname');
    }

    return $url_parts['scheme'] . '://' . $hostname . $this->aliasManager->getAliasByPath('/' . $cloned_url->getInternalPath(), $langCode);
  }

}

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

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