Skip to main content

Tell us about your project

A schematics showing the process of creating sitemap

A Sitemap is a list of all Web pages from a Website, and it's very useful because it helps your content appear faster in the user's browser. There are two types of Sitemap, static and dynamic.

Static Sitemaps are being used today only for static Websites, or for Websites that do not have a lot of pages.

Creating these Sitemaps could take a long time because after you create every new page on your Website you'll have to manually include it to the Sitemap. On the other hand, dynamic Sitemaps are much more useful and used. They are connected with the Database of the Website, and every time a new page is created, it will automatically be included in the Sitemap.

Drupal 8 has one great module, which allows us to quickly and easily generate a Sitemap for our Website. It is a Simple XML Sitemap, which you can download here. The installation of this module is not complicated, and nor is using it.

In its user interface, we can choose which entities we would like to include in the Sitemap.

For example, if we choose entity type node as one of them, we can also choose which of the node content types we would like to exclude from the Sitemap, and also which nodes we'd like to exclude. After every change, we need to run the "Regenerate sitemap" script, which you can find on the module configuration page. ("admin/config/search/simplesitemap"). The sitemap will then be created under your project root folder, and you can access it by running this link: "your_project/sitemap.xml".

As I have already said, this module is very helpful, but what if we would like to generate our map which requires some other specification, for example, if we would like to include only the nodes created after some specific date, or we need  to include the nodes created by some specific user(s).

We could still manage to do that with the module's interface, but what if there are more than a hundred, or even a thousand nodes we needed to change. It would be such a pain if you had to do this manually. On the other hand, it would be a lot easier if we could create some custom code, which then would regenerate the Sitemap with our changes.

I'll explain in this blog how to make this happen.

In order to follow the code examples, you will need to have a custom module.

If you don't have any, you'll have to create one. For this tutorial, I'll name mine custom_sitemap.

In the custom_sitemap.module file, I'll create two functions:

  1. function custom_sitemap_sitemap_link($uri, $lang, $changed, $priority) {
  2.   $base_url = \Drupal::request()->getSchemeAndHttpHost();
  3.   $path = $base_url . $uri;
  4.   $link = custom_sitemap_sitemap_link_markup($path, $changed, $priority, $uri);
  6.   return $link;
  8. }
  10. function custom_sitemap_sitemap_link_markup($path, $changed, $priority, $uri) {
  12.   $base_url = \Drupal::request()->getSchemeAndHttpHost();
  14.   return '<url>
  15.   <loc>' . $path . '</loc>
  16.   <xhtml:link href="' . $base_url . $uri . '" hreflang="en" rel="alternate">
  17.   <lastmod>' . $changed . '</lastmod>
  18.   <priority>' . $priority . '</priority>
  19.    </xhtml:link></url>';
  21. }

In order to create our custom values for the Sitemap, I'll need to retrieve the values which will represent the URLs of pages which will be included in the Sitemap, language the node text was written in, timestamp value which will represent the value when the node was changed, and priority (supports values from 0.1 to 1.0) for the Sitemap.

Then I'll create a controller in the src folder, which should be in the root folder of your module ( if you don't have it, create a folder named "Controller" ), and therein I`ll create a PHP file "MapgenerateController.php". This controller will be invoked every time when the script "/map-generate" is created ( which I will later define in my module.routing file ), and I will create a service that this module will use to retrieve the results.

  1. <?php
  2. namespace Drupal\simple_sitemap\Controller;
  4. use Drupal\Core\Controller\ControllerBase;
  6. class MapgenerateController extends ControllerBase {
  8.   /**
  9.    * Generates sitemap.
  10.    *
  11.    * @return array
  12.    *   A simple renderable array.
  13.    */
  14.   public static function generate() {
  15.     $sitemap = \Drupal::service('simple_sitemap.sitemap');
  16.     $sitemap->generate();
  18.     $element = array(
  19.       '#markup' => 'Sitemap generated.',
  20.     );
  21.     return $element;
  22.   }
  23. }

In the previous code, I've only defined a service that will be called after every script run, and I have defined a markup element on the page which will be loaded after the script has run.

The next step is creating and defining a custom route for our controller. In the file custom_sitemap.routing.yml I`ve inserted the code below:

  1. custom_sitemap.map_generate:
  3.   path: '/map-generate'
  5.   defaults:
  7.     _controller: '\Drupal\custom_sitemap\Controller\MapgenerateController::generate'
  9.     _title: 'Generate sitemap'
  11.   requirements:
  13.     _permission: 'access toolbar'

I have defined the path for our controller, which will execute the function "generate" after running the script "/map-generate".

Also, I need to create the service, which I have defined in our Controller file. It's very simple, in a file ( in my case: ), insert the code below:

  1. services:
  3.  custom_sitemap.sitemap:
  5.  class: Drupal\custom_sitemap\Sitemap
  7.  public: true

In the previous code, I've defined our service custom_sitemap.sitemap, which will use the class I still need to create. 

The path of the class is: your_project/modules/custom_sitemap/src, the file name in my case is: Sitemap.php.

  1. <?php
  2. namespace Drupal\custom_sitemap;
  4. use Drupal\Core\Entity;
  5. use Drupal\node\Entity\Node;
  6. use Drupal\Core\Controller\ControllerBase;
  7. use Drupal\Core\Render;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  10. /**
  11.  * Class Sitemap.
  12.  *
  13.  * @package Drupal\custom_sitemap
  14.  */
  15. class Sitemap {
  17.     public function __construct() {
  19.     }
  21.     public function generate() {
  22.         $base_url = \Drupal::request()->getSchemeAndHttpHost();
  24.         $string = \Drupal::database()-&gt;select('simple_sitemap', 'sm')
  25.         ->fields('sm', array('sitemap_string'))
  26.        ->condition('', 1)
  27.         ->execute()->fetchField();
  28.         $string = substr($string, 0, -10);
  31.         $sql = "SELECT title, langcode from node_field_data n
  32.                             WHERE n.type = "page" and n.uid = 1";
  33.         $query = db_query($sql);      
  35.         $results = $query-&gt;fetchAll();
  37.         foreach($results as $result){
  39.                 $changed = time();
  40.                 $changed = date('c', $changed);
  42.                 $langcode = $result-&gt;langcode;
  44.                 $name = $result-&gt;title;
  46.                 $name = str_replace(array(' '), array('-'), strtolower($name));    
  48.                 $uri = '/'. $name;
  50.                 $string .= custom_sitemap_sitemap_link($uri, $langcode, $changed, 0.7);                    
  52.         }
  53.             $string .= '
  54.            ';
  56.             if ($handle = fopen('sitemap.xml', 'w')) {
  57.                 if (fwrite($handle, $string) === FALSE)
  58.                     print 'Cannot write to file';
  59.                     fclose($handle);
  60.             }
  61.         }
  63.     }

This class is probably the most important part of making a custom Sitemap, because there lays some basic logic of the custom integration.

Firstly I have selected the current content of the Sitemap from the Database, which is generated using the Simple XML Sitemap module. Then I've created an SQL query which will select all nodes from the database of type "Page" and which are created by the user with uid 1 (admin). This is just a demo example, you can write the SQL query whatever way you need.

In order to regenerate the Sitemap correctly with our custom values, I need to add the URL, langcode, and the timestamp when the node was changed defined, as well as the priority value for the Sitemap.

To that effect, I will select only the title and langcode of those nodes from the Database. The name values will be redefined for URL purposes, they will be converted to small letters using the function strtolower, and strings ' ' (empty space) will be replaced with '-' (dashes) using function str_replace.

I'll set that the value of "changed" timestamp is the moment of the running of the script, and the priority value is 0.7.

Again, these are optional values, you can set them however you want, based on your needs.

I'll write those changes in the sitemap.xml text file later.

Now, after every script run "your_domain/map-generate" your Sitemap will be regenerated with your newly added custom results. It would be highly recommended that you also create a cron directive, where you can define that your script runs automatically and periodically, whenever you want. If you don't already have a cron function in your custom module, create one and insert the code below:

  1. function custom_sitemap_cron() {
  2.   $time = date('Hi', time());
  4.   // Once a day, just after the midnight.
  5.   if ($time >= '0000' &amp;&amp; $time <= '0059') {
  6.     // Generate sitemap.
  7.     $generator = \Drupal::service('simple_sitemap.generator');
  8.     $generator->generateSitemap('backend');
  10.     $sitemap = \Drupal::service('custom_sitemap.sitemap');
  11.     $sitemap->generate();
  13.   }
  14. }

In the previous code, I`ve defined that my custom cron will run every day, just after midnight, and it will run the Regenerate sitemap script that the Simple XML sitemap module is using, along with your newly added script.


Lazar Padjan