You are here

class SitemapController in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 src/Controller/SitemapController.php \Drupal\sitemap\Controller\SitemapController
  2. 8 src/Controller/SitemapController.php \Drupal\sitemap\Controller\SitemapController

Controller routines for update routes.

Hierarchy

Expanded class hierarchy of SitemapController

File

src/Controller/SitemapController.php, line 13

Namespace

Drupal\sitemap\Controller
View source
class SitemapController implements ContainerInjectionInterface {

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * The SitemapMap plugin manager.
   *
   * @var \Drupal\sitemap\SitemapManager
   */
  protected $sitemapManager;

  /**
   * Constructs update status data.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The configuration factory.
   * @param \Drupal\sitemap\SitemapManager $sitemap_manager
   *   The SitemapMap plugin manager.
   */
  public function __construct(ConfigFactoryInterface $config_factory, SitemapManager $sitemap_manager) {
    $this->configFactory = $config_factory;
    $this->sitemapManager = $sitemap_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('plugin.manager.sitemap'));
  }

  /**
   * Controller for /sitemap.
   *
   * @return array
   *   Renderable array.
   */
  public function buildSitemap() {
    $config = $this->configFactory
      ->get('sitemap.settings');

    // Build the Sitemap message.
    $message = '';
    if (!empty($config
      ->get('message')) && !empty($config
      ->get('message')['value'])) {
      $message = check_markup($config
        ->get('message')['value'], $config
        ->get('message')['format']);
    }

    // Build the plugin content.
    $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) {

          // TODO: Order by $instance->weight

          //$plugins[$instance->weight] = $instance->view();
          $plugins[] = $instance
            ->view();
        }
      }
    }

    // Build the render array.
    $sitemap = [
      '#theme' => 'sitemap',
      '#message' => $message,
      '#sitemap_items' => $plugins,
    ];

    // Check whether to include the default CSS.
    if ($config
      ->get('include_css') == 1) {
      $sitemap['#attached']['library'] = [
        'sitemap/sitemap.theme',
      ];
    }
    return $sitemap;
  }

  /**
   * Returns sitemap page's title.
   *
   * @return string
   *   Sitemap page title.
   */
  public function getTitle() {
    $config = $this->configFactory
      ->get('sitemap.settings');
    return $config
      ->get('page_title');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SitemapController::$configFactory protected property The configuration factory.
SitemapController::$sitemapManager protected property The SitemapMap plugin manager.
SitemapController::buildSitemap public function Controller for /sitemap.
SitemapController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
SitemapController::getTitle public function Returns sitemap page's title.
SitemapController::__construct public function Constructs update status data.