You are here

function cms_content_sync_update_8010 in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x cms_content_sync.install \cms_content_sync_update_8010()
  2. 2.0.x cms_content_sync.install \cms_content_sync_update_8010()

Promote Site ID + Authentication type as a side-wide property rather than a setting per pool.

File

./cms_content_sync.install, line 323
Install file for cms_content_sync.

Code

function cms_content_sync_update_8010() {

  /**
   * Part 1: Authentication type. Saved as configuration so it can be deployed.
   */
  $authentication_type = NULL;
  $config_factory = Drupal::configFactory();

  // Check that all Site IDs are set to the same value in all pools.
  foreach (Pool::getAll() as $pool) {

    // In case someone runs this update late.
    if (!$pool->authentication_type) {
      continue;
    }
    if (!$authentication_type || $pool->authentication_type === IApplicationInterface::AUTHENTICATION_TYPE_BASIC_AUTH) {
      $authentication_type = $pool->authentication_type;
    }
  }
  if ($authentication_type) {
    $config_factory
      ->getEditable('cms_content_sync.settings')
      ->set('cms_content_sync_authentication_type', $authentication_type)
      ->save();
  }

  // Unset duplicate data now.
  foreach (Pool::getAll() as $pool) {
    $pool->authentication_type = NULL;
    $pool
      ->save();
  }

  /**
   * Part 2: Site ID. Saved as State so it can't be deployed.
   */
  $site_id = NULL;
  $pool_site_id = NULL;
  $cms_content_sync_settings = Settings::get('cms_content_sync');

  // Check that all Site IDs are set to the same value in all pools.
  foreach (Pool::getAll() as $pool) {

    // Check for overwritten pool site_id.
    if (isset($cms_content_sync_settings) && isset($cms_content_sync_settings['pools'][$pool->id]['site_id'])) {
      $pool_site_id = $cms_content_sync_settings['pools'][$pool->id]['site_id'];
    }
    else {
      $pool_site_id = $pool->site_id;
    }

    // In case someone runs this update late.
    if (!$pool_site_id) {
      continue;
    }
    if ($site_id) {
      if ($site_id !== $pool_site_id) {
        throw new Exception('Site ID must be unique per site. Please update your pool configuration or settings.php configuration for the pool overrides to reflect that and try again.');
      }
    }
    else {
      $site_id = $pool_site_id;
    }
  }
  if ($site_id) {
    Drupal::state()
      ->set('cms_content_sync.site_machine_name', $site_id);
  }

  // Verify everything's okay. Will also export the site's name for better usability.
  foreach (SyncCoreFactory::getAllSyncCores() as $core) {
    $core
      ->registerSite();
  }

  // Unset duplicate data now.
  foreach (Pool::getAll() as $pool) {
    $pool->site_id = NULL;
    $pool
      ->save();
  }
  return 'Changed site ID and authentication type to be set per site, not per pool.';
}