You are here

protected function SimpleSitemapStorage::doSave in Simple XML sitemap 4.x

Performs storage-specific saving of the entity.

Parameters

int|string $id: The original entity ID.

\Drupal\Core\Entity\EntityInterface $entity: The entity to save.

Return value

bool|int If the record insert or update failed, returns FALSE. If it succeeded, returns SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides ConfigEntityStorage::doSave

File

src/Entity/SimpleSitemapStorage.php, line 130

Class

SimpleSitemapStorage

Namespace

Drupal\simple_sitemap\Entity

Code

protected function doSave($id, EntityInterface $entity) {

  /** @var SimpleSitemapInterface $entity */
  if (!preg_match('/^[\\w\\-_]+$/', $id)) {
    throw new \InvalidArgumentException("The sitemap ID can only include alphanumeric characters, dashes and underscores.");
  }
  if ($entity
    ->get('type') === NULL || $entity
    ->get('type') === '') {
    throw new \InvalidArgumentException("The sitemap must define its sitemap type information.");
  }
  if ($this->entityTypeManager
    ->getStorage('simple_sitemap_type')
    ->load($entity
    ->get('type')) === NULL) {
    throw new \InvalidArgumentException("Sitemap type {$entity->get('type')} does not exist.");
  }
  if ($entity
    ->label() === NULL || $entity
    ->label() === '') {
    $entity
      ->set('label', $id);
  }
  if ($entity
    ->get('weight') === NULL || $entity
    ->get('weight') === '') {
    $entity
      ->set('weight', 0);
  }
  return parent::doSave($id, $entity);
}