SimplesitemapController.php 3.75 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1 2
<?php

gbyte.co's avatar
gbyte.co committed
3
namespace Drupal\simple_sitemap\Controller;
gbyte.co's avatar
gbyte.co committed
4

5 6
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
gbyte.co's avatar
gbyte.co committed
7
use Symfony\Component\HttpFoundation\Response;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
gbyte.co's avatar
gbyte.co committed
9
use Drupal\simple_sitemap\Simplesitemap;
10
use Symfony\Component\HttpFoundation\Request;
11
use Drupal\simple_sitemap\SimplesitemapManager;
gbyte.co's avatar
gbyte.co committed
12 13

/**
gbyte.co's avatar
gbyte.co committed
14
 * Class SimplesitemapController
gbyte.co's avatar
gbyte.co committed
15
 * @package Drupal\simple_sitemap\Controller
gbyte.co's avatar
gbyte.co committed
16
 */
17 18 19 20 21
class SimplesitemapController extends ControllerBase {

  /**
   * @var \Drupal\simple_sitemap\Simplesitemap
   */
22
  protected $generator;
gbyte.co's avatar
gbyte.co committed
23

24 25
  /**
   * SimplesitemapController constructor.
26
   * @param \Drupal\simple_sitemap\Simplesitemap $generator
27
   */
28
  public function __construct(Simplesitemap $generator) {
29
    $this->generator = $generator;
30 31 32 33 34 35 36
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
37
      $container->get('simple_sitemap.generator')
38
    );
39 40
  }

gbyte.co's avatar
gbyte.co committed
41
  /**
42
   * Returns the whole sitemap variant, its requested chunk,
43
   * or its sitemap index file.
44
   * Caches the response in case of expected output, prevents caching otherwise.
45
   *
46 47 48
   * @param \Symfony\Component\HttpFoundation\Request $request
   *  The request object.
   *
49 50
   * @param string $variant
   *  Optional name of sitemap variant.
51
   *  @see SimplesitemapManager::getSitemapVariants()
52
   *
53 54
   * @throws NotFoundHttpException
   *
55
   * @return \Symfony\Component\HttpFoundation\Response|false
56
   *  Returns an XML response.
gbyte.co's avatar
gbyte.co committed
57
   */
58
  public function getSitemap(Request $request, $variant = NULL) {
59
    $output = $this->generator->setVariants($variant)->getSitemap($request->query->getInt('page'));
60 61 62
    if (!$output) {
      throw new NotFoundHttpException();
    }
gbyte.co's avatar
gbyte.co committed
63

64
    return new Response($output, Response::HTTP_OK, [
65 66
      'Content-type' => 'application/xml; charset=utf-8',
      'X-Robots-Tag' => 'noindex, follow',
67
    ]);
68
  }
69 70

  /**
71
   * Returns the XML stylesheet for the sitemap.
72 73
   *
   * @return \Symfony\Component\HttpFoundation\Response
74 75
   */
  public function getSitemapXsl() {
76

77 78 79 80 81 82 83
    // Read the XSL content from the file.
    $module_path = drupal_get_path('module', 'simple_sitemap');
    $xsl_content = file_get_contents($module_path . '/xsl/simple_sitemap.xsl');

    // Replace custom tokens in the XSL content with appropriate values.
    $replacements = [
      '[title]' => $this->t('Sitemap file'),
84
      '[generated-by]' => $this->t('Generated by the <a href="@link">@module_name</a> Drupal module.', ['@link' => 'https://www.drupal.org/project/simple_sitemap', '@module_name' => 'Simple XML Sitemap']),
85 86 87 88 89 90 91 92 93 94
      '[number-of-sitemaps]' => $this->t('Number of sitemaps in this index'),
      '[sitemap-url]' => $this->t('Sitemap URL'),
      '[number-of-urls]' => $this->t('Number of URLs in this sitemap'),
      '[url-location]' => $this->t('URL location'),
      '[lastmod]' => $this->t('Last modification date'),
      '[changefreq]' => $this->t('Change frequency'),
      '[priority]' => $this->t('Priority'),
      '[translation-set]' => $this->t('Translation set'),
      '[images]' => $this->t('Images'),
      '[jquery]' => base_path() . 'core/assets/vendor/jquery/jquery.min.js',
95
      '[jquery-tablesorter]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
96
      '[parser-date-iso8601]' => base_path() . $module_path . '/xsl/parser-date-iso8601.min.js',
97 98 99 100 101
      '[xsl-js]' => base_path() . $module_path . '/xsl/simple_sitemap.xsl.js',
      '[xsl-css]' => base_path() . $module_path . '/xsl/simple_sitemap.xsl.css',
    ];

    // Output the XSL content.
102 103 104 105
    return new Response(strtr($xsl_content, $replacements), Response::HTTP_OK, [
      'Content-type' => 'application/xml; charset=utf-8',
      'X-Robots-Tag' => 'noindex, nofollow',
    ]);
106 107
  }

gbyte.co's avatar
gbyte.co committed
108
}