You are here

public function VersionComparison::doComparisonOnCreate in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x modules/cms_content_sync_developer/src/EventSubscriber/VersionComparison.php \Drupal\cms_content_sync_developer\EventSubscriber\VersionComparison::doComparisonOnCreate()
  2. 2.0.x modules/cms_content_sync_developer/src/EventSubscriber/VersionComparison.php \Drupal\cms_content_sync_developer\EventSubscriber\VersionComparison::doComparisonOnCreate()

Check for config changes on create.

Parameters

\Drupal\Core\Config\ConfigCrudEvent $event: The Event to process.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

modules/cms_content_sync_developer/src/EventSubscriber/VersionComparison.php, line 53

Class

VersionComparison
A subscriber triggering a config when certain configuration changes.

Namespace

Drupal\cms_content_sync_developer\EventSubscriber

Code

public function doComparisonOnCreate(ConfigCrudEvent $event) {

  // Comparison is not done if config got changed by CLI.
  if (in_array(PHP_SAPI, [
    'cli',
    'cli-server',
    'phpdbg',
  ])) {
    return;
  }
  $saved_config = $event
    ->getConfig();
  $new_config = $saved_config
    ->getRawData();
  $old_config = $saved_config
    ->getOriginal();

  // Check if the config has changed and that we got a new configuration.
  if ($new_config === $old_config || empty($new_config)) {
    return;
  }

  // Entity Type and bundle are not set consistent between the entity types.
  $entity_type = $this
    ->getEntityTypeFromConfig($new_config);
  $bundle = $this
    ->getBundleFromConfig($new_config);
  if (!isset($entity_type) || !isset($bundle)) {
    return;
  }
  $flows = Flow::getAll();
  $mismatching_flows = [];
  foreach ($flows as $flow_id => $flow) {

    // We only need to compare the version for the effected entity type.
    $entity_type_config = $flow
      ->getEntityTypeConfig($entity_type, $bundle);
    if ($entity_type_config && $entity_type_config['handler'] != Flow::HANDLER_IGNORE) {
      $current_version = $entity_type_config['version'];
      $new_version = $flow
        ->getEntityTypeVersion($entity_type_config['entity_type_name'], $entity_type_config['bundle_name']);
      if ($current_version != $new_version) {
        $mismatching_flows[$flow_id] = $flow
          ->label();
      }
    }
    else {

      // If no entity type config exists for now,
      // we assume that it is a new entity type.
      $configs = $flow->sync_entities;
      foreach ($configs as $config_id => $config) {
        if (isset($config['handler_settings']['export_referenced_entities']) && $config['handler_settings']['export_referenced_entities']) {

          // Check if there is a reference field handler that
          // automatically pushes other bundles of this entity type.
          preg_match('/^([^-]+)-([^-]+)-([^-]+)$/', $config_id, $matches);
          $config_entity_type_name = $matches[1];
          $config_field_name = $matches[3];
          $field_storage = $this->entity_type_manager
            ->getStorage('field_storage_config')
            ->load($config_entity_type_name . '.' . $config_field_name);
          if ($field_storage instanceof FieldStorageConfig) {
            $settings = $field_storage
              ->get('settings');
            if ($settings['target_type'] == $entity_type) {
              $mismatching_flows[$flow_id] = $flow
                ->label();
            }
          }
        }
      }
    }
  }

  // Set the mismatching flows.
  if (!empty($mismatching_flows)) {
    $this
      ->setMismatchingFlows($mismatching_flows);
  }
}