The way to accomplish this is to create a SitemapGenerator plugin and implement getXml().

The problem:
If extending DefaultSitemapGenerator, there is no way to add custom properties to each entry. Instead, the entire code of DefaultSitemapGenerator::getXml() must be copied and modified.

Proposed solution:
Move the code that is currently inside the foreach ($links as $link) loop into its own method. It could be called something like processLinkForXml() or writeUrl, since it is creating a single url node with some child nodes.

I think it makes sense to make this change in DefaultSitemapGenerator, so that if others only want to add properties, the solution is to extend the class, override writeUrl (or whatever it's named), call parent::writeUrl(), then make other additions to the XML node.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nadavoid created an issue. See original summary.

gbyte’s picture

Good idea, looking forward to a patch!

nadavoid’s picture

Status: Active » Needs review
FileSize
4.84 KB

Patch attached. Used buildUrl() as the function name.

Tested locally in an extending class, using code like this:

  protected function buildUrl(array $link) {
    parent::buildUrl($link);
    // Add custom metadata for Mything.
    $this->writer->startElement('mything:metadata');
    // Keys are controlled in mymodule_simple_sitemap_links_alter().
    foreach ($link['mything:metadata'] as $key => $value) {
      $this->writer->writeElement($key, $value);
    }
    $this->writer->endElement();
  }

  • gbyte.co committed da5c237 on 8.x-3.x
    Issue #3062820 by nadavoid, gbyte.co: Enable addition of custom XML...
gbyte’s picture

Status: Needs review » Fixed

I've taken it further and compartmentalized a little more. Please test dev and get back to me.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.