Showing posts with label custom sitemap link. Show all posts
Showing posts with label custom sitemap link. 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. 



Wednesday 26 August 2020

How to remove or unset url links form simple sitemap in druapl 8?

Simple way to unset the URL link is alter simple sitemap module, here we will give an example of code for remove or unset any link on any condition basis.

Example:

Create a custom module or put the code in existing custom module.

a. First render class library in file header.

use Drupal\simple_sitemap\Simplesitemap;

b. Then write the hook function code.

/**

 * Alter the generated link data before the sitemap is saved.

 * This hook gets invoked for every sitemap chunk generated.

 *

 * @param array &$links

 *   Array containing multilingual links generated for each path to be indexed.

 */

function finder_simple_sitemap_links_alter(&$links) {


  // Remove french URL for a certain path in the hreflang sitemap.

  foreach ($links as $key => $link) {

    if ($link['path'] === 'node/1') {

      // Remove 'loc' URL if it points to a french site.

      if ($link['langcode'] === 'fr') {

        unset($links[$key]);

      }

      // If this 'loc' URL points to a non-french site, make sure to remove

      // its french alternate URL.

      else {

        if ($link['alternate_urls']['fr']) {

          unset($links[$key]['alternate_urls']['fr']);

        }

      }

    }

  }

}


Note: If have any suggestions or issue regarding 'How to remove or unset url links form simple sitemap in druapl 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...