public static function FlowControllerPerBundle::createFlow in CMS Content Sync 2.1.x
Create a flow configuration programmatically.
Parameters
$flow_name:
string $flow_id:
bool $status:
array $dependencies:
$configurations:
bool $force_update:
Return value
mixed|string
File
- src/
Controller/ FlowControllerPerBundle.php, line 67
Class
Namespace
Drupal\cms_content_sync\ControllerCode
public static function createFlow($flow_name, $flow_id = '', $status = true, $dependencies = [], $configurations, $force_update = false) {
$flows = Flow::getAll(true);
// 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 . ' already exists. Creation has been skipped.', 'warning');
return $flow_id;
}
$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', $status)
->set('id', $flow_id)
->set('name', $flow_name)
->set('type', Flow::TYPE_PUSH)
->set('variant', Flow::VARIANT_PER_BUNDLE)
->set('config', $config)
->set('per_bundle_settings', []);
// 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('per_bundle_settings.' . $entity_type_key . '.' . $bundle_key . '.settings', [
'handler' => $entity_handler['id'],
'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('per_bundle_settings.' . $entity_type_key . '.' . $bundle_key . '.properties.' . $field_id, $field_config);
}
}
}
$flow_config
->save();
return $flow_id;
}