You are here

class SitemapWriter in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/simple_sitemap/SitemapGenerator/SitemapWriter.php \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter

Class SitemapWriter

Hierarchy

  • class \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter extends \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\XMLWriter

Expanded class hierarchy of SitemapWriter

1 string reference to 'SitemapWriter'
simple_sitemap.services.yml in ./simple_sitemap.services.yml
simple_sitemap.services.yml
1 service uses SitemapWriter
simple_sitemap.sitemap_writer in ./simple_sitemap.services.yml
Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\SitemapWriter

File

src/Plugin/simple_sitemap/SitemapGenerator/SitemapWriter.php, line 10

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator
View source
class SitemapWriter extends \XMLWriter {
  protected const GENERATED_BY = 'Generated by the Simple XML Sitemap Drupal module: https://drupal.org/project/simple_sitemap.';
  protected const XML_VERSION = '1.0';
  protected const ENCODING = 'UTF-8';

  /**
   * @var \Drupal\Core\Routing\RouteProvider
   */
  protected $routeProvider;
  public function __construct(RouteProviderInterface $route_provider) {
    $this->routeProvider = $route_provider;
  }

  /**
   * Adds the XML stylesheet to the XML page.
   */
  public function writeXsl() : void {

    // Using this instead of URL::fromRoute() to avoid creating a path with the
    // subdomain from which creation was triggered which might lead to a CORS
    // problem. See https://www.drupal.org/project/simple_sitemap/issues/3131672.
    $xsl_url = $this->routeProvider
      ->getRouteByName('simple_sitemap.sitemap_xsl')
      ->getPath();

    // The above workaround however generates an incorrect path when the site is
    // located in a subdirectory, which is why the following logic adds the base
    // path of the installation.
    // See https://www.drupal.org/project/simple_sitemap/issues/3154494.
    // All of this seems to be an over engineered way of writing 'sitemap.xsl',
    // but may be useful in cases where another module alters the routes.
    $xsl_url = base_path() . ltrim($xsl_url, '/');
    $this
      ->writePI('xml-stylesheet', 'type="text/xsl" href="' . $xsl_url . '"');
  }
  public function writeGeneratedBy() : void {
    $this
      ->writeComment(self::GENERATED_BY);
  }
  public function startSitemapDocument() : void {
    $this
      ->startDocument(self::XML_VERSION, self::ENCODING);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SitemapWriter::$routeProvider protected property
SitemapWriter::ENCODING protected constant
SitemapWriter::GENERATED_BY protected constant
SitemapWriter::startSitemapDocument public function
SitemapWriter::writeGeneratedBy public function
SitemapWriter::writeXsl public function Adds the XML stylesheet to the XML page.
SitemapWriter::XML_VERSION protected constant
SitemapWriter::__construct public function