0

I am trying to generate Sitemap for my site using Simple XML Sitemap module. I have checked the Documentation for Extending the module features. Since I am newbie to Drupal 8, I am in confusion how to get this achieved. tried some hooks but failed it may be my lack of understanding regarding drupal8 module development. Can anyone help me to get this done. What are the namespaces I should add to my custom module.

Currently my URLs are generated like www.mysite.com/country/india but I want it to be like www.mysite.com/country/india/ since I use trailing slash with URLs.

I've used htaccess to add trailing slash to URLs with 301 redirect.

2 Answers 2

2

You can use hook_simple_sitemap_links_alter() to change the links generated before the sitemap is saved.

function example_simple_sitemap_links_alter(&$links) {
  global $base_url;
  foreach ($links as $key => $link) {
    if ($links[$key]['url'] != $base_url . '/') { // Excluding base url
      $links[$key]['url'] = $link['url'] . '/'; // Here Trailing slash will be added
    }
  }
}

Before using the hook:

enter image description here

After using the hook:

enter image description here

5
  • hi where should i add this if i develop a custom module in drupal 8?In Drupal 7 its like adding this with module name is same works in Drupal 8?
    – Rosh_LK
    Commented May 8, 2018 at 7:54
  • Yes. But the folder structure is different in D8. Kindly check this drupal.org/docs/8/creating-custom-modules/basic-structure
    – Ajay Reddy
    Commented May 8, 2018 at 8:02
  • Did you found how to use above code in your custom module?
    – Ajay Reddy
    Commented May 8, 2018 at 8:40
  • If your site is multilingual, you may also want to alter the alternate URLs in the same hook. $links[$key]['alternate_urls'] is an array of alternate URLs belonging to the main URL. Check out the API example: cgit.drupalcode.org/simple_sitemap/tree/simple_sitemap.api.php
    – gbyte
    Commented Nov 30, 2018 at 23:40
  • if you are working with xmlsitemap, the right hook would be xmlsitemap_link_url_alter() Commented Dec 6, 2021 at 9:32
-1

have a look at this ... it does not do exactly what you want but could be a good place to start.

  hook_sitemap_menu_tree_main_alter(array &$menu) {
      /**@var \Drupal\Core\Menu\MenuLinkTreeElement $menuLink **/
     foreach ($menu as $menuLink) {
       dump($menuLink); <-- do stuff with this 
     }
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.