You are here

public function XmlSitemapLinkStorage::save in XML sitemap 2.x

Same name and namespace in other branches
  1. 8 src/XmlSitemapLinkStorage.php \Drupal\xmlsitemap\XmlSitemapLinkStorage::save()

Saves or updates a sitemap link.

Parameters

array $link: An array with a sitemap link.

Overrides XmlSitemapLinkStorageInterface::save

File

src/XmlSitemapLinkStorage.php, line 139

Class

XmlSitemapLinkStorage
XmlSitemap link storage service class.

Namespace

Drupal\xmlsitemap

Code

public function save(array $link, array $context = []) {
  $link += [
    'access' => 1,
    'status' => 1,
    'status_override' => 0,
    'lastmod' => 0,
    'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
    'priority_override' => 0,
    'changefreq' => 0,
    'changecount' => 0,
    'language' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ];

  // Allow other modules to alter the link before saving.
  $this->moduleHandler
    ->alter('xmlsitemap_link', $link, $context);

  // Temporary validation checks.
  // @todo Remove in final?
  if ($link['priority'] < 0 || $link['priority'] > 1) {
    trigger_error("The XML sitemap link for {$link['type']} {$link['id']} has an invalid priority of {$link['priority']}.<br/>" . var_export($link, TRUE), E_USER_ERROR);
  }
  if ($link['changecount'] < 0) {
    trigger_error("The XML sitemap link for {$link['type']} {$link['id']} has a negative changecount value. Please report this to https://www.drupal.org/node/516928.<br/>" . var_export($link, TRUE), E_USER_ERROR);
    $link['changecount'] = 0;
  }

  // Throw an error with the link does not start with a slash.
  // @see \Drupal\Core\Url::fromInternalUri()
  if ($link['loc'][0] !== '/') {
    trigger_error("The XML sitemap link path {$link['loc']} for {$link['type']} {$link['id']} is invalid because it does not start with a slash.", E_USER_ERROR);
  }

  // Check if this is a changed link and set the regenerate flag if necessary.
  if (!$this->state
    ->get('xmlsitemap_regenerate_needed')) {
    $this
      ->checkChangedLink($link, NULL, TRUE);
  }
  $queryStatus = $this->connection
    ->merge('xmlsitemap')
    ->keys([
    'type' => $link['type'],
    'id' => $link['id'],
    'language' => $link['language'],
  ])
    ->fields([
    'loc' => $link['loc'],
    'subtype' => $link['subtype'],
    'access' => (int) $link['access'],
    'status' => (int) $link['status'],
    'status_override' => $link['status_override'],
    'lastmod' => $link['lastmod'],
    'priority' => $link['priority'],
    'priority_override' => $link['priority_override'],
    'changefreq' => $link['changefreq'],
    'changecount' => $link['changecount'],
  ])
    ->execute();
  switch ($queryStatus) {
    case Merge::STATUS_INSERT:
      $this->moduleHandler
        ->invokeAll('xmlsitemap_link_insert', [
        $link,
        $context,
      ]);
      break;
    case Merge::STATUS_UPDATE:
      $this->moduleHandler
        ->invokeAll('xmlsitemap_link_update', [
        $link,
        $context,
      ]);
      break;
  }
  return $link;
}