You are here

public function VersionComparison::doComparisonOnCreate in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 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
      ->getController()
      ->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, $bundle);
      if ($current_version != $new_version) {
        $mismatching_flows[$flow_id] = $flow
          ->label();
      }
    }
    else {

      // We can ignore variant Simple because it will set this automatically
      // based on whether the target entity type is enabled or not.
      if ($flow->variant === Flow::VARIANT_PER_BUNDLE) {

        // If no entity type config exists for now, we assume that it is a new
        // entity type.
        $types = $flow->per_bundle_settings;
        foreach ($types as $entity_type_name => $bundles) {
          foreach ($bundles as $bundle) {
            foreach ($bundle['properties'] as $field_name => $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.
                $field_storage = $this->entity_type_manager
                  ->getStorage('field_storage_config')
                  ->load($entity_type_name . '.' . $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);
  }
}