SimplesitemapController.php 3.67 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 string $variant
   *  Optional name of sitemap variant.
   *  @see \hook_simple_sitemap_variants_alter()
49
   *  @see SimplesitemapManager::getSitemapVariants()
50
   *
51 52
   * @param \Symfony\Component\HttpFoundation\Request $request
   *  The request object.
53
   *
54 55
   * @throws NotFoundHttpException
   *
gbyte.co's avatar
gbyte.co committed
56
   * @return object
57
   *  Returns an XML response.
gbyte.co's avatar
gbyte.co committed
58
   */
59
  public function getSitemap(Request $request, $variant = NULL) {
60
    $output = $this->generator->setVariants($variant)->getSitemap($request->query->getInt('page'));
61 62 63
    if (!$output) {
      throw new NotFoundHttpException();
    }
gbyte.co's avatar
gbyte.co committed
64

65
    return new Response($output, Response::HTTP_OK, [
66
      'content-type' => 'application/xml',
67
      'X-Robots-Tag' => 'noindex', // Tell search engines not to index the sitemap itself.
68
    ]);
69
  }
70 71

  /**
72
   * Returns the XML stylesheet for the sitemap.
73 74
   */
  public function getSitemapXsl() {
75

76 77 78 79 80 81 82
    // 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'),
83
      '[generated-by]' => $this->t('Generated by the <a href="@link">Simple XML sitemap</a> Drupal module.', ['@link' => 'https://www.drupal.org/project/simple_sitemap']),
84 85 86 87 88 89 90 91 92 93
      '[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',
94
      '[jquery-tablesorter]' => base_path() . $module_path . '/xsl/jquery.tablesorter.min.js',
95 96 97 98 99 100 101 102 103
      '[xsl-js]' => base_path() . $module_path . '/xsl/simple_sitemap.xsl.js',
      '[xsl-css]' => base_path() . $module_path . '/xsl/simple_sitemap.xsl.css',
    ];
    $xsl_content = strtr($xsl_content, $replacements);

    // Output the XSL content.
    $response = new Response($xsl_content);
    $response->headers->set('Content-type', 'application/xml; charset=utf-8');
    $response->headers->set('X-Robots-Tag', 'noindex, follow');
104

105 106 107
    return $response;
  }

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