You are here

public function Simplesitemap::setBundleSettings in Simple XML sitemap 8.3

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

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

$entity_type_id:

null $bundle_name:

array $settings:

Return value

$this

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Simplesitemap.php, line 478

Class

Simplesitemap
Class Simplesitemap @package Drupal\simple_sitemap

Namespace

Drupal\simple_sitemap

Code

public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings = [
  'index' => TRUE,
]) {
  if (empty($variants = $this
    ->getVariants(FALSE))) {
    return $this;
  }
  $bundle_name = NULL !== $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('entity', $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->db
      ->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->db
        ->delete('simple_sitemap_entity_overrides')
        ->condition('id', $delete_instances, 'IN')
        ->execute();
    }
  }
  return $this;
}