You are here

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

Sets settings for bundle or non-bundle entity types. This is done for the currently set variant. Note that this method takes only the first set variant into account. See todo.

@todo multiple variants

Parameters

string $entity_type_id:

string|null $bundle_name:

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 149

Class

EntityManager
Class EntityManager

Namespace

Drupal\simple_sitemap\Manager

Code

public function setBundleSettings(string $entity_type_id, ?string $bundle_name = NULL, array $settings = [
  'index' => TRUE,
]) : EntityManager {
  if (empty($variants = $this
    ->getVariants(FALSE))) {
    return $this;
  }
  $bundle_name = $bundle_name ?? $entity_type_id;
  if (!empty($old_settings = $this
    ->getBundleSettings($entity_type_id, $bundle_name))) {
    $settings = array_merge($old_settings, $settings);
  }
  self::supplementDefaultSettings($settings);
  if ($settings != $old_settings) {

    // Save new bundle settings to configuration.
    $bundle_settings = $this->configFactory
      ->getEditable("simple_sitemap.bundle_settings.{$variants[0]}.{$entity_type_id}.{$bundle_name}");
    foreach ($settings as $setting_key => $setting) {
      $bundle_settings
        ->set($setting_key, $setting);
    }
    $bundle_settings
      ->save();
    if (empty($entity_ids = $this->entityHelper
      ->getEntityInstanceIds($entity_type_id, $bundle_name))) {
      return $this;
    }

    // Delete all entity overrides in case bundle indexation is disabled.
    if (empty($settings['index'])) {
      $this
        ->removeEntityInstanceSettings($entity_type_id, $entity_ids);
      return $this;
    }

    // Delete entity overrides which are identical to new bundle settings.
    // todo Enclose into some sensible method.
    $query = $this->database
      ->select('simple_sitemap_entity_overrides', 'o')
      ->fields('o', [
      'id',
      'inclusion_settings',
    ])
      ->condition('o.entity_type', $entity_type_id)
      ->condition('o.type', $variants[0]);
    if (!empty($entity_ids)) {
      $query
        ->condition('o.entity_id', $entity_ids, 'IN');
    }
    $delete_instances = [];
    foreach ($query
      ->execute()
      ->fetchAll() as $result) {
      $delete = TRUE;
      $instance_settings = unserialize($result->inclusion_settings);
      foreach ($instance_settings as $setting_key => $instance_setting) {
        if ($instance_setting != $settings[$setting_key]) {
          $delete = FALSE;
          break;
        }
      }
      if ($delete) {
        $delete_instances[] = $result->id;
      }
    }
    if (!empty($delete_instances)) {

      // todo Use removeEntityInstanceSettings() instead.
      $this->database
        ->delete('simple_sitemap_entity_overrides')
        ->condition('id', $delete_instances, 'IN')
        ->execute();
    }
  }
  return $this;
}