You are here

public function CustomLinkManager::add in Simple XML sitemap 4.x

Stores a custom path along with its settings to configuration for the currently set variants.

@todo Validate $settings and throw exceptions

Parameters

string $path:

array $settings: Settings that are not provided are supplemented by defaults.

Return value

\Drupal\simple_sitemap\Manager\CustomLinkManager

File

src/Manager/CustomLinkManager.php, line 56

Class

CustomLinkManager
Class CustomLinkManager

Namespace

Drupal\simple_sitemap\Manager

Code

public function add(string $path, array $settings = []) : CustomLinkManager {
  if (empty($variants = $this
    ->getVariants(FALSE))) {
    return $this;
  }
  if (!(bool) $this->pathValidator
    ->getUrlIfValidWithoutAccessCheck($path)) {

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

    // todo: log error.
    return $this;
  }
  $variant_links = $this
    ->get(NULL, FALSE, TRUE);
  foreach ($variants as $variant) {
    $links = [];
    $link_key = 0;
    if (isset($variant_links[$variant])) {
      $links = $variant_links[$variant];
      $link_key = count($links);
      foreach ($links as $key => $link) {
        if ($link['path'] === $path) {
          $link_key = $key;
          break;
        }
      }
    }
    $links[$link_key] = [
      'path' => $path,
    ] + $settings;
    $this->configFactory
      ->getEditable("simple_sitemap.custom_links.{$variant}")
      ->set('links', $links)
      ->save();
  }
  return $this;
}