SitemapGenerator.php 3.56 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
<?php
/**
 * @file
 * Contains \Drupal\simplesitemap\SitemapGenerator.
 *
 * Generates a sitemap for entities and custom links.
 */

namespace Drupal\simplesitemap;

use Drupal\simplesitemap\LinkGenerators\CustomLinkGenerator;
12
use \XMLWriter;
13 14 15 16 17 18 19 20 21 22 23

/**
 * SitemapGenerator class.
 */
class SitemapGenerator {

  const PRIORITY_DEFAULT = 0.5;
  const PRIORITY_HIGHEST = 10;
  const PRIORITY_DIVIDER = 10;
  const XML_VERSION = '1.0';
  const ENCODING = 'UTF-8';
24 25
  const XMLNS = 'http://www.sitemaps.org/schemas/sitemap/0.9';
  const XMLNS_XHTML = 'http://www.w3.org/1999/xhtml';
26 27 28 29

  private $entity_types;
  private $custom;
  private $links;
30 31 32 33 34 35
  private $languages;

  function __construct() {
    $this->languages = \Drupal::languageManager()->getLanguages();
    $this->links = array();
  }
36 37 38 39 40 41 42 43 44 45 46

  public static function get_priority_select_values() {
    $options = array();
    foreach(range(0, self::PRIORITY_HIGHEST) as $value) {
      $value = $value / self::PRIORITY_DIVIDER;
      $options[(string)$value] = (string)$value;
    }
    return $options;
  }

  public function set_entity_types($entity_types) {
47
    $this->entity_types = is_array($entity_types) ? $entity_types : array();
48 49 50
  }

  public function set_custom_links($custom) {
51
    $this->custom = is_array($custom) ? $custom : array();
52 53 54 55 56 57
  }

  public function generate_sitemap() {

    $this->generate_custom_links();
    $this->generate_entity_links();
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

    $default_language_id = \Drupal::languageManager()->getDefaultLanguage()->getId();

    $writer = new XMLWriter();
    $writer->openMemory();
    $writer->setIndent(TRUE);
    $writer->startDocument(self::XML_VERSION, self::ENCODING);
    $writer->startElement('urlset');
    $writer->writeAttribute('xmlns', self::XMLNS);
    $writer->writeAttribute('xmlns:xhtml', self::XMLNS_XHTML);

    foreach ($this->links as $link) {
      $writer->startElement('url');

      // Adding url to standard language.
      $writer->writeElement('loc', $link['url'][$default_language_id]);

      // Adding alternate urls (other languages).
      if (count($link['url']) > 1) {
        foreach($link['url'] as $language_id => $localised_url) {
            $writer->startElement('xhtml:link');
            $writer->writeAttribute('rel', 'alternate');
            $writer->writeAttribute('hreflang', $language_id);
            $writer->writeAttribute('href', $localised_url);
            $writer->endElement();
        }
      }

      // Add priority.
      if (!is_null($link['priority'])) {
        $writer->writeElement('priority', $link['priority']);
      }

      // Add lastmod.
      if (!is_null($link['lastmod'])) {
        $writer->writeElement('lastmod', $link['lastmod']);
      }
      $writer->endElement();
    }
    $writer->endDocument();
    return $writer->outputMemory();
99 100 101 102 103
  }

  // Add custom links.
  private function generate_custom_links() {
    $link_generator = new CustomLinkGenerator();
104
    $links = $link_generator->get_custom_links($this->custom , $this->languages);
Pawel G's avatar
Pawel G committed
105
    $this->links = array_merge($this->links, $links);
106 107 108 109 110 111 112 113 114 115
  }

  // Add entity type links.
  private function generate_entity_links() {
    foreach($this->entity_types as $entity_type => $bundles) {
      $class_path = Simplesitemap::get_plugin_path($entity_type);
      if ($class_path !== FALSE) {
        require_once $class_path;
        $class_name = "Drupal\\simplesitemap\\LinkGenerators\\EntityTypeLinkGenerators\\$entity_type";
        $link_generator = new $class_name();
116
        $links = $link_generator->get_entity_links($entity_type, $bundles, $this->languages);
Pawel G's avatar
Pawel G committed
117
        $this->links = array_merge($this->links, $links);
118 119 120 121
      }
    }
  }
}