You are here

public function Simplesitemap::setEntityInstanceSettings in Simple XML sitemap 8.2

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

Overrides entity bundle/entity type sitemap settings for a single entity.

Parameters

string $entity_type_id:

int $id:

array $settings:

Return value

$this

File

src/Simplesitemap.php, line 499

Class

Simplesitemap
Class Simplesitemap @package Drupal\simple_sitemap

Namespace

Drupal\simple_sitemap

Code

public function setEntityInstanceSettings($entity_type_id, $id, $settings) {
  $entity = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->load($id);
  $bundle_settings = $this
    ->getBundleSettings($entity_type_id, $this->entityHelper
    ->getEntityInstanceBundleName($entity));
  if (!empty($bundle_settings)) {

    // Check if overrides are different from bundle setting before saving.
    $override = FALSE;
    foreach ($settings as $key => $setting) {
      if (!isset($bundle_settings[$key]) || $setting != $bundle_settings[$key]) {
        $override = TRUE;
        break;
      }
    }

    // Save overrides for this entity if something is different.
    if ($override) {
      $this->db
        ->merge('simple_sitemap_entity_overrides')
        ->key([
        'entity_type' => $entity_type_id,
        'entity_id' => $id,
      ])
        ->fields([
        'entity_type' => $entity_type_id,
        'entity_id' => $id,
        'inclusion_settings' => serialize(array_merge($bundle_settings, $settings)),
      ])
        ->execute();
    }
    else {
      $this
        ->removeEntityInstanceSettings($entity_type_id, $id);
    }
  }
  else {

    //todo: log error
  }
  return $this;
}