Showing posts with label generate xml in drupal 8. Show all posts
Showing posts with label generate xml in drupal 8. Show all posts

Thursday 24 September 2020

How to generate sitemap xml in drupal 8 ?

Simple way to generate XML for sitemap in Drupal 8, we have 'Simple sitemap' and 'XML sitemap' modules but if we want  to add some custom code or want customization in XML, so we have simple code for generate custom XML, please follow the given process and example.

Steps:

  1. Create custom module and controller file and render the below given class in file header.
    use Drupal\Core\Controller\ControllerBase;
    use Drupal\node\Entity\Node;
    use Symfony\Component\HttpFoundation\Response;

  2. Write the code in controller function:
    $rows .= '<?xml version="1.0" encoding="UTF-8" ?>';

    //open the urlset..
    $rows .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"
                                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
                                http://www.google.com/schemas/sitemap/0.84/sitemap.xsd">';

    If you want to add COVEO elements in XML then we have to use below given code also:

     $rows .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84"   
                                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                 xsi:schemaLocation="http://www.google.com/schemas/sitemap/0.84
                                 http://www.google.com/schemas/sitemap/0.84/sitemap.xsd"
                                 xmlns:coveo="http://www.coveo.com/schemas/metadata">';

  3. After that create the xml code with elements:
    $rows.='<url>
                 <loc>'.$base_url.''.$alias.'</loc>
                 <lastmod>'.$last_modified.'</lastmod>
                 <changefreq>daily</changefreq>
                 <priority>0.5</priority>
                 <coveo:metadata>
                         <md_contenttype>'.$res_content_type.'</md_contenttype>                         
                         <md_pagetype>'.$pagetype.'</md_pagetype>
                 </coveo:metadata>
                 </url>';

    $rows .='</urlset>'; //close the urlset..

  4. After that last final code for render XML:
         $response = new Response();
         $response->headers->set('Content-Type', 'text/xml'); //add content type = text/xml header.. 
         $response->setContent($rows); // pass the $row as array or xml print value..
         return $response; //return reponse with xml format without any styling..

    So with the help of this simple code we can create custom XML in drupal 8.


Note: If have any suggestions or issue regarding 'How to generate sitemap xml in drupal 8 ?' then you can ask by comments. 



How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...