You are here

public static function Flow::createFlow in CMS Content Sync 2.0.x

Same name and namespace in other branches
  1. 8 src/Entity/Flow.php \Drupal\cms_content_sync\Entity\Flow::createFlow()

Create a flow configuration programmatically.

Parameters

$flow_name:

string $flow_id:

bool $status:

array $dependencies:

$configurations:

bool $force_update:

Return value

mixed|string

3 calls to Flow::createFlow()
FlowFormExpert::submitForm in modules/cms_content_sync_developer/src/Form/FlowFormExpert.php
MigratePull::createFlow in modules/cms_content_sync_migrate_acquia_content_hub/src/Form/MigratePull.php
Create the CMS Content Hub pull flow for the content hub filter.
MigratePush::createFlow in modules/cms_content_sync_migrate_acquia_content_hub/src/Form/MigratePush.php

File

src/Entity/Flow.php, line 1108

Class

Flow
Defines the "Content Sync - Flow" entity.

Namespace

Drupal\cms_content_sync\Entity

Code

public static function createFlow($flow_name, $flow_id = '', $status = true, $dependencies = [], $configurations, $force_update = false) {
  $flows = Flow::getAll(false);

  // If no flow_id is given, create one.
  if (empty($flow_id)) {
    $flow_id = strtolower($flow_name);
    $flow_id = preg_replace('@[^a-z0-9_]+@', '_', $flow_id);
  }
  if (!$force_update && array_key_exists($flow_id, $flows)) {
    \Drupal::messenger()
      ->addMessage('A flow with the machine name ' . $flow_id . ' does already exist. Therefor the creation has been skipped.', 'warning');
  }
  else {
    $uuid_service = \Drupal::service('uuid');
    $language_manager = \Drupal::service('language_manager');
    $default_language = $language_manager
      ->getDefaultLanguage();
    $config = [
      'dependencies' => $dependencies,
    ];
    $flow_config = \Drupal::service('config.factory')
      ->getEditable('cms_content_sync.flow.' . $flow_id);

    // Setup base configurations.
    $flow_config
      ->set('uuid', $uuid_service
      ->generate())
      ->set('langcode', $default_language
      ->getId())
      ->set('status', true)
      ->set('id', $flow_id)
      ->set('name', $flow_name)
      ->set('config', $config)
      ->set('sync_entities', []);

    // Configure entity types.
    foreach ($configurations as $entity_type_key => $bundles) {
      foreach ($bundles as $bundle_key => $bundle) {
        $entityPluginManager = \Drupal::service('plugin.manager.cms_content_sync_entity_handler');
        $entity_handler = $entityPluginManager
          ->getHandlerOptions($entity_type_key, $bundle_key);
        $entity_handler = reset($entity_handler);

        // Set configurations.
        $flow_config
          ->set('sync_entities.' . $entity_type_key . '-' . $bundle_key, [
          'handler' => $entity_handler['id'],
          'entity_type_name' => $entity_type_key,
          'bundle_name' => $bundle_key,
          'version' => Flow::getEntityTypeVersion($entity_type_key, $bundle_key),
          'export' => $bundle['push_configuration']['behavior'] ?? PushIntent::PUSH_DISABLED,
          'export_deletion_settings' => [
            'export_deletion' => $bundle['push_configuration']['export_deletion_settings'] ?? '',
          ],
          'export_pools' => $bundle['push_configuration']['export_pools'] ?? [],
          'import' => $bundle['import_configuration']['behavior'] ?? PullIntent::PULL_DISABLED,
          'import_deletion_settings' => [
            'import_deletion' => $bundle['import_configuration']['import_deletion'] ?? 0,
            'allow_local_deletion_of_import' => $bundle['import_configuration']['allow_local_deletion_of_import'] ?? 0,
          ],
          'import_updates' => $bundle['import_configuration']['import_updates'] ?? PullIntent::PULL_UPDATE_FORCE,
          'import_pools' => $bundle['import_configuration']['import_pools'] ?? [],
          'pool_export_widget_type' => 'checkboxes',
          'preview' => 'table',
        ]);

        /**
         * @var \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
         */
        $entityFieldManager = \Drupal::service('entity_field.manager');
        $fields = $entityFieldManager
          ->getFieldDefinitions($entity_type_key, $bundle_key);
        foreach (Flow::getDefaultFieldConfigForEntityType($entity_type_key, $bundle_key) as $field_id => $field_config) {
          if (!empty($bundle['tags'])) {
            list(, , $field_name) = explode('-', $field_id);
            $field = $fields[$field_name];
            if ($field && 'entity_reference' == $field
              ->getType() && 'taxonomy_term' == $field
              ->getSetting('target_type')) {
              $bundles = $field
                ->getSetting('target_bundles');
              if (!$bundles) {
                $field_settings = $field
                  ->getSettings();
                $bundles = $field_settings['handler_settings']['target_bundles'];
              }
              if (is_array($bundles)) {
                foreach ($bundle['tags'] as $tag) {
                  if (in_array($tag
                    ->bundle(), $bundles)) {
                    $field_config['handler_settings']['subscribe_only_to'][] = [
                      'type' => 'taxonomy_term',
                      'bundle' => $tag
                        ->bundle(),
                      'uuid' => $tag
                        ->uuid(),
                    ];
                  }
                }
              }
            }
          }
          $flow_config
            ->set('sync_entities.' . $field_id, $field_config);
        }
      }
    }
    $flow_config
      ->save();
  }
  return $flow_id;
}