You are here

public function EntityManager::setEntityInstanceSettings in Simple XML sitemap 4.x

Overrides sitemap settings for a single entity for the currently set variants.

@todo Check functionality (variant setting etc). @todo Pass entity object instead of id and bundle.

Parameters

string $entity_type_id:

string $id:

array $settings:

Return value

\Drupal\simple_sitemap\Manager\EntityManager

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Manager/EntityManager.php, line 338

Class

EntityManager
Class EntityManager

Namespace

Drupal\simple_sitemap\Manager

Code

public function setEntityInstanceSettings(string $entity_type_id, string $id, array $settings) : EntityManager {
  if (empty($this
    ->getVariants(FALSE))) {
    return $this;
  }
  if (($entity = $this->entityTypeManager
    ->getStorage($entity_type_id)
    ->load($id)) === NULL) {

    // todo exception
    return $this;
  }
  $all_bundle_settings = $this
    ->getBundleSettings($entity_type_id, $this->entityHelper
    ->getEntityInstanceBundleName($entity), TRUE, TRUE);
  foreach ($all_bundle_settings as $variant => $bundle_settings) {
    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->database
          ->merge('simple_sitemap_entity_overrides')
          ->keys([
          'type' => $variant,
          'entity_type' => $entity_type_id,
          'entity_id' => $id,
        ])
          ->fields([
          'type' => $variant,
          'entity_type' => $entity_type_id,
          'entity_id' => $id,
          'inclusion_settings' => serialize(array_merge($bundle_settings, $settings)),
        ])
          ->execute();
      }
      else {
        $this
          ->removeEntityInstanceSettings($entity_type_id, $id);
      }
    }
  }
  return $this;
}