SimplesitemapController.php 1.7 KB
Newer Older
gbyte.co's avatar
gbyte.co committed
1 2 3
<?php
/**
 * @file
gbyte.co's avatar
gbyte.co committed
4
 * Contains \Drupal\simple_sitemap\Controller\SimplesitemapController.
gbyte.co's avatar
gbyte.co committed
5 6
 */

gbyte.co's avatar
gbyte.co committed
7
namespace Drupal\simple_sitemap\Controller;
gbyte.co's avatar
gbyte.co committed
8

9
use Drupal\Core\Cache\CacheableResponse;
10 11
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
gbyte.co's avatar
gbyte.co committed
12 13 14 15 16
use Symfony\Component\HttpFoundation\Response;

/**
 * SimplesitemapController.
 */
17 18 19 20 21 22 23 24
class SimplesitemapController extends ControllerBase {

  /**
   * The sitemap generator.
   *
   * @var \Drupal\simple_sitemap\Simplesitemap
   */
  protected $sitemapGenerator;
gbyte.co's avatar
gbyte.co committed
25

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

gbyte.co's avatar
gbyte.co committed
36
  /**
37
   * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file.
38 39
   *
   * @param int $sitemap_id
40 41
   *  Optional ID of the sitemap chunk. If none provided, the first chunk or
   *  the sitemap index is fetched.
42 43 44
   *
   * @return object Response
   *  Returns an XML response.
gbyte.co's avatar
gbyte.co committed
45
   */
46
  public function getSitemap($sitemap_id = NULL) {
47
    $output = $this->sitemapGenerator->getSitemap($sitemap_id);
48
    $output = !$output ? '' : $output;
gbyte.co's avatar
gbyte.co committed
49

50
    // Display sitemap with correct xml header.
51 52 53 54
    $response = new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
    $meta_data = $response->getCacheableMetadata();
    $meta_data->addCacheTags(['simple_sitemap']);
    return $response;
gbyte.co's avatar
gbyte.co committed
55
  }
56 57 58 59 60 61 62 63

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

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