Question

I am trying to find how I can debug/kint a variable/array from the Simple XML Sitemap?

I worked through the documentation here: https://www.drupal.org/docs/8/modules/simple-xml-sitemap/api-and-extending-the-module#s-api-hooks to find the hook I need.

My goal is to unset any links that have node/ to remove published, but un-aliased nodes of included content types.

The array key ['path'] looks to be the unaliased URL and the below code removes all links except the home page. I am unsure how I can kint($link) in this function so I can see what other array keys are available to see what else I may use for comparison.

function HOOK_simple_sitemap_links_alter(array &$links, $sitemap_variant) {

  foreach ($links as $key => $link) {
    if (strpos($link['meta']['path'], 'node/') !== FALSE) {
      unset($links[$key]);
    }
  }
}

Is there a way to kint() these sitemap arrays? Or maybe some documentation that shows the structure of these arrays?

Was it helpful?

Solution

As Clive said, you will need to make sure that both the devel and kint modules are enabled. (NOTE: D8's Devel 4.0 now includes the kint module but requires 'composer require kint-php/kint' to be done to make it work) Then you can add your hook and just do a ksm($links); statement.

Make sure to clear cache so the hook will be found. Then doing a regenerate of the sitemap(s) in the gui will product the kint output that you can examine for the structure.

Also, not totally sure, but I think this hook has to be in a module and will not work in the .theme file.

FYI - Here's a code snippet from a working hook that does what you want, including removing untranslated nodes.

/**
 * Implements hook_simple_sitemap_links_alter().
 */
function my_module_simple_sitemap_links_alter(&$links, $variant) {
  foreach($links as $key => $link) {
    // Filter out any node/### URLs
    if ( preg_match("!(/[a-z-]*)?/node/\d?!", $link['url']) ) {
      unset( $links[$key] );
      continue;
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top