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

Pawel G's avatar
Pawel G committed
7
namespace Drupal\simple_sitemap\Controller;
Pawel G's avatar
Pawel G committed
8

9
use Drupal\Core\Cache\CacheableResponse;
Pawel G's avatar
Pawel G committed
10
use Symfony\Component\HttpFoundation\Response;
Pawel G's avatar
Pawel G committed
11
use Drupal\simple_sitemap\Simplesitemap;
Pawel G's avatar
Pawel G committed
12 13 14 15 16 17 18

/**
 * SimplesitemapController.
 */
class SimplesitemapController {

  /**
19
   * Returns the whole sitemap, a requested sitemap chunk, or the sitemap index file.
20 21
   *
   * @param int $sitemap_id
22 23
   *  Optional ID of the sitemap chunk. If none provided, the first chunk or
   *  the sitemap index is fetched.
24 25 26
   *
   * @return object Response
   *  Returns an XML response.
Pawel G's avatar
Pawel G committed
27
   */
28
  public function get_sitemap($sitemap_id = NULL) {
29
    $sitemap = new Simplesitemap;
30
    $output = $sitemap->get_sitemap($sitemap_id);
31
    $output = !$output ? '' : $output;
Pawel G's avatar
Pawel G committed
32

33
    // Display sitemap with correct xml header.
34 35 36 37
    $response = new CacheableResponse($output, Response::HTTP_OK, array('content-type' => 'application/xml'));
    $meta_data = $response->getCacheableMetadata();
    $meta_data->addCacheTags(['simple_sitemap']);
    return $response;
Pawel G's avatar
Pawel G committed
38 39
  }
}