1

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?

2
  • 1
    Assuming you have devel installed and kint selected for output, it should be as easy as ksm($links); to print the kint output to the message area
    – Clive
    Sep 25, 2020 at 9:29
  • I had kint enabled but not devel. kint() throws errors while regenerating the sitemap links, but ksm() does not and displays what is needed. Sep 25, 2020 at 14:50

1 Answer 1

3

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;
    }
}
1
  • ['url'] was the key I was looking for. I am using if (strstr($link['url'], '/node/')), but this answers the need of my Q, and the above will remove /node/ URL's. ksm() seemed to be the big difference in debugging. Sep 25, 2020 at 14:53

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.