You are here

protected function DefaultSitemapGenerator::addUrl in Simple XML sitemap 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php \Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator\DefaultSitemapGenerator::addUrl()

Adds a URL element to the sitemap.

Parameters

array $url_data: The array of properties for this URL.

1 call to DefaultSitemapGenerator::addUrl()
DefaultSitemapGenerator::addLinks in src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php
Adds URL elements to the sitemap.

File

src/Plugin/simple_sitemap/SitemapGenerator/DefaultSitemapGenerator.php, line 91

Class

DefaultSitemapGenerator
Class DefaultSitemapGenerator

Namespace

Drupal\simple_sitemap\Plugin\simple_sitemap\SitemapGenerator

Code

protected function addUrl(array $url_data) : void {
  $this->writer
    ->writeElement('loc', $url_data['url']);

  // If more than one language is enabled, add all translation variant URLs
  // as alternate links to this link turning the sitemap into a hreflang
  // sitemap.
  if (isset($url_data['alternate_urls']) && $this->sitemapVariant
    ->isMultilingual()) {
    $this
      ->addAlternateUrls($url_data['alternate_urls']);
  }

  // Add lastmod if any.
  if (isset($url_data['lastmod'])) {
    $this->writer
      ->writeElement('lastmod', $url_data['lastmod']);
  }

  // Add changefreq if any.
  if (isset($url_data['changefreq'])) {
    $this->writer
      ->writeElement('changefreq', $url_data['changefreq']);
  }

  // Add priority if any.
  if (isset($url_data['priority'])) {
    $this->writer
      ->writeElement('priority', $url_data['priority']);
  }

  // Add images if any.
  if (!empty($url_data['images'])) {
    foreach ($url_data['images'] as $image) {
      $this->writer
        ->startElement('image:image');
      $this->writer
        ->writeElement('image:loc', $image['path']);
      if (strlen($image['title']) > 0) {
        $this->writer
          ->writeElement('image:title', $image['title']);
      }
      if (strlen($image['alt']) > 0) {
        $this->writer
          ->writeElement('image:caption', $image['alt']);
      }
      $this->writer
        ->endElement();
    }
  }
}