You are here

public function Simplesitemap::setBundleSettings in Simple XML sitemap 8.2

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

Sets sitemap settings for a non-bundle entity type (e.g. user) or a bundle of an entity type (e.g. page).

@todo: enableEntityType automatically

Parameters

string $entity_type_id: Entity type id like 'node' the bundle belongs to.

string $bundle_name: Name of the bundle. NULL if entity type has no bundles.

array $settings: An array of sitemap settings for this bundle/entity type. Example: ['index' => TRUE, 'priority' => 0.5, 'changefreq' => 'never', 'include_images' => FALSE].

Return value

$this

File

src/Simplesitemap.php, line 370

Class

Simplesitemap
Class Simplesitemap @package Drupal\simple_sitemap

Namespace

Drupal\simple_sitemap

Code

public function setBundleSettings($entity_type_id, $bundle_name = NULL, $settings = []) {
  $bundle_name = empty($bundle_name) ? $entity_type_id : $bundle_name;
  if (!empty($old_settings = $this
    ->getBundleSettings($entity_type_id, $bundle_name))) {
    $settings = array_merge($old_settings, $settings);
  }
  else {
    self::supplementDefaultSettings('entity', $settings);
  }
  $bundle_settings = $this->configFactory
    ->getEditable("simple_sitemap.bundle_settings.{$entity_type_id}.{$bundle_name}");
  foreach ($settings as $setting_key => $setting) {
    if ($setting_key === 'index') {
      $setting = intval($setting);
    }
    $bundle_settings
      ->set($setting_key, $setting);
  }
  $bundle_settings
    ->save();

  // Delete entity overrides which are identical to new bundle setting.
  $sitemap_entity_types = $this->entityHelper
    ->getSupportedEntityTypes();
  if (isset($sitemap_entity_types[$entity_type_id])) {
    $entity_type = $sitemap_entity_types[$entity_type_id];
    $keys = $entity_type
      ->getKeys();

    // Menu fix.
    $keys['bundle'] = $entity_type_id === 'menu_link_content' ? 'menu_name' : $keys['bundle'];
    $query = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->getQuery();
    if (!$this->entityHelper
      ->entityTypeIsAtomic($entity_type_id)) {
      $query
        ->condition($keys['bundle'], $bundle_name);
    }
    $entity_ids = $query
      ->execute();
    $query = $this->db
      ->select('simple_sitemap_entity_overrides', 'o')
      ->fields('o', [
      'id',
      'inclusion_settings',
    ])
      ->condition('o.entity_type', $entity_type_id);
    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)) {
      $this->db
        ->delete('simple_sitemap_entity_overrides')
        ->condition('id', $delete_instances, 'IN')
        ->execute();
    }
  }
  else {

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