You are here

public function Simplesitemap::addCustomLink in Simple XML sitemap 8.2

Same name and namespace in other branches
  1. 8.3 src/Simplesitemap.php \Drupal\simple_sitemap\Simplesitemap::addCustomLink()

Stores a custom path along with its sitemap settings to configuration.

@todo Validate $settings and throw exceptions

Parameters

string $path:

array $settings:

Return value

$this

File

src/Simplesitemap.php, line 622

Class

Simplesitemap
Class Simplesitemap @package Drupal\simple_sitemap

Namespace

Drupal\simple_sitemap

Code

public function addCustomLink($path, $settings = []) {
  if (!$this->pathValidator
    ->isValid($path)) {

    // todo: log error.
    return $this;
  }
  if ($path[0] !== '/') {

    // todo: log error.
    return $this;
  }
  $custom_links = $this
    ->getCustomLinks(FALSE);
  foreach ($custom_links as $key => $link) {
    if ($link['path'] === $path) {
      $link_key = $key;
      break;
    }
  }
  $link_key = isset($link_key) ? $link_key : count($custom_links);
  $custom_links[$link_key] = [
    'path' => $path,
  ] + $settings;
  $this->configFactory
    ->getEditable('simple_sitemap.custom')
    ->set('links', $custom_links)
    ->save();
  return $this;
}