SitemapController.php in Sitemap 2.0.x
File
src/Controller/SitemapController.php
View source
<?php
namespace Drupal\sitemap\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\sitemap\SitemapManager;
class SitemapController implements ContainerInjectionInterface {
protected $configFactory;
protected $sitemapManager;
public function __construct(ConfigFactoryInterface $config_factory, SitemapManager $sitemap_manager) {
$this->configFactory = $config_factory;
$this->sitemapManager = $sitemap_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('plugin.manager.sitemap'));
}
public function buildSitemap() {
$config = $this->configFactory
->get('sitemap.settings');
$message = '';
if (!empty($config
->get('message')) && !empty($config
->get('message')['value'])) {
$message = check_markup($config
->get('message')['value'], $config
->get('message')['format']);
}
$plugins_config = $config
->get('plugins');
$plugins = [];
$plugin_config = [];
$definitions = $this->sitemapManager
->getDefinitions();
foreach ($definitions as $id => $definition) {
if ($this->sitemapManager
->hasDefinition($id)) {
if (!empty($plugins_config[$id])) {
$plugin_config = $plugins_config[$id];
}
$instance = $this->sitemapManager
->createInstance($id, $plugin_config);
if ($instance->enabled) {
$plugins[] = $instance
->view();
}
}
}
$sitemap = [
'#theme' => 'sitemap',
'#message' => $message,
'#sitemap_items' => $plugins,
];
if ($config
->get('include_css') == 1) {
$sitemap['#attached']['library'] = [
'sitemap/sitemap.theme',
];
}
return $sitemap;
}
public function getTitle() {
$config = $this->configFactory
->get('sitemap.settings');
return $config
->get('page_title');
}
}