public function CopyRemoteFlow::save in CMS Content Sync 8
Same name and namespace in other branches
- 2.1.x src/Form/CopyRemoteFlow.php \Drupal\cms_content_sync\Form\CopyRemoteFlow::save()
- 2.0.x src/Form/CopyRemoteFlow.php \Drupal\cms_content_sync\Form\CopyRemoteFlow::save()
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Form/ CopyRemoteFlow.php, line 175
Class
- CopyRemoteFlow
- Form handler for the Pool add and edit forms.
Namespace
Drupal\cms_content_sync\FormCode
public function save(array $form, FormStateInterface $form_state) {
$url = $form_state
->getValue('url') ? $form_state
->getValue('url') : $_GET['url'];
$flow = $form_state
->getValue('flow');
$flow = $this
->getFlowConfig($url, $flow);
$data = Yaml::decode($flow
->getConfig());
$sync_entities = $data['sync_entities'];
$entity_types = $this->bundleInfoService
->getAllBundleInfo();
// Remove entity type and field configurations that don't exist locally.
foreach ($sync_entities as $name => $settings) {
$parts = explode('-', $name);
list($entity_type_name, $bundle_name) = $parts;
if (empty($entity_types[$entity_type_name][$bundle_name])) {
unset($sync_entities[$name]);
}
}
$values = $form_state
->getValues();
// First replace pool settings (as we might be about to swap the whole push/pull settings below)
foreach ($values as $name => $setting) {
$parts = explode('_', $name);
$action = $parts[0];
if ('export' === $action || 'import' === $action) {
$subaction = $parts[2];
if ('pool' !== $subaction) {
continue;
}
$sync_setting = $parts[1];
$old_pool_setting = $parts[3];
$old_pool = implode('_', array_slice($parts, 4));
$setting_parts = explode('_', $setting);
$new_pool_setting = $setting_parts[0];
$new_pool = implode('_', array_slice($setting_parts, 1));
foreach ($sync_entities as $sync_name => $sync_settings) {
// We only care for entity type settings.
if (1 !== substr_count($sync_name, '-')) {
continue;
}
if ($sync_settings[$action] !== $sync_setting) {
continue;
}
if (empty($sync_settings[$action . '_pools'][$old_pool]) || $sync_settings[$action . '_pools'][$old_pool] !== $old_pool_setting) {
continue;
}
$sync_entities[$sync_name][$action . '_pools'][$new_pool] = $new_pool_setting;
if ($old_pool !== $new_pool && !empty($sync_entities[$sync_name][$action . '_pools'][$old_pool])) {
// If someone uses it to replace A with B *AND* B with A, then this will break it depending on the order of
// the array. But we don't care about that use case yet.
// Changing this also means we have to care about circular references and run it X times where X is the
// number of pool + action associations possible.
unset($sync_entities[$sync_name][$action . '_pools'][$old_pool]);
}
}
}
}
$action_reverse = [
'export' => [
PushIntent::PUSH_DISABLED => PullIntent::PULL_DISABLED,
PushIntent::PUSH_AUTOMATICALLY => PullIntent::PULL_AUTOMATICALLY,
PushIntent::PUSH_MANUALLY => PullIntent::PULL_MANUALLY,
PushIntent::PUSH_AS_DEPENDENCY => PullIntent::PULL_AS_DEPENDENCY,
],
'import' => [
PullIntent::PULL_DISABLED => PushIntent::PUSH_DISABLED,
PullIntent::PULL_AUTOMATICALLY => PushIntent::PUSH_AUTOMATICALLY,
PullIntent::PULL_MANUALLY => PushIntent::PUSH_MANUALLY,
PullIntent::PULL_AS_DEPENDENCY => PushIntent::PUSH_AS_DEPENDENCY,
],
];
// Next replace the actual sync settings. This might include swapping push and pull completely.
foreach ($values as $name => $setting) {
$parts = explode('_', $name);
$action = $parts[0];
if ('export' === $action || 'import' === $action) {
$subaction = $parts[2];
if ('becomes' !== $subaction) {
continue;
}
$sync_setting = $parts[1];
$setting_parts = explode('_', $setting);
$new_action = $setting_parts[0];
$new_sync_setting = $setting_parts[1];
foreach ($sync_entities as $sync_name => $sync_settings) {
// We only care for entity type settings.
if (1 !== substr_count($sync_name, '-')) {
continue;
}
if ($sync_settings[$action] !== $sync_setting) {
continue;
}
// Swap pools in case push+pull changed; This works because $sync_settings is not a reference but a copy.
$sync_entities[$sync_name][$new_action . '_pools'] = $sync_settings[$action . '_pools'];
if (!empty($sync_settings[$new_action . '_pools'])) {
$sync_entities[$sync_name][$action . '_pools'] = $sync_settings[$new_action . '_pools'];
}
// Swap "Push / Pull deletion" settings as well.
if (!empty($sync_settings[$action . '_deletion_settings'])) {
$sync_entities[$sync_name][$new_action . '_deletion_settings'][$new_action . '_deletion'] = $sync_settings[$action . '_deletion_settings'][$action . '_deletion'];
}
if (!empty($sync_settings[$new_action . '_deletion_settings'])) {
$sync_entities[$sync_name][$action . '_deletion_settings'][$action . '_deletion'] = $sync_settings[$new_action . '_deletion_settings'][$new_action . '_deletion'];
}
// Set new sync setting (e.g. manually becomes automatically)
$sync_entities[$sync_name][$new_action] = $new_sync_setting;
// In case push and pull was swapped, we want to actually swap these settings...
if ($action !== $new_action) {
// ...unless this will also be handled by our foreach. In this case we leave it untouched.
if (empty($values[$new_action . '_' . $new_sync_setting . '_becomes'])) {
// So now when the user swaps pull automatically with Push automatically for example with Push being
// disabled in the flow that's copied, this will make sure that now the pull is disabled.
$sync_entities[$sync_name][$action] = $action_reverse[$new_action][$sync_settings[$new_action]];
}
// Swap field push and pull settings if required.
foreach ($sync_entities as $field_sync_name => $field_sync_settings) {
// We only care for field settings for this entity type.
if (substr($field_sync_name, 0, strlen($sync_name) + 1) !== $sync_name . '-') {
continue;
}
// Swap settings if push+pull changed; This works because $sync_settings is not a reference but a copy.
if (!empty($field_sync_settings[$new_action])) {
$sync_entities[$field_sync_name][$action] = $action_reverse[$new_action][$field_sync_settings[$new_action]];
}
if (!empty($field_sync_settings[$action])) {
$sync_entities[$field_sync_name][$new_action] = $action_reverse[$new_action][$field_sync_settings[$action]];
}
}
}
}
}
}
$values['id'] = $form_state
->getValue('id');
$values['name'] = $form_state
->getValue('name');
$values['sync_entities'] = $sync_entities;
$flow = Flow::create($values);
$flow
->save();
// Make sure that the flow is now edited, saved and then exported. This is critical because some settings require
// new defaults, e.g. if you change pull to push you want to set "Push referenced entities" settings now.
\Drupal::request()->query
->remove('destination');
$form_state
->setRedirect('entity.cms_content_sync_flow.edit_form', [
'cms_content_sync_flow' => $flow
->id(),
], [
'query' => [
'open' => '',
],
]);
}