Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New event when TV values have been synchronized #204

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion _build/resolvers/resolve.babelevents.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ function removeEvent(modX $modx, $name)
$babelEvents = [
'OnBabelDuplicate', // invoked on duplicating the resource in a new language context
'OnBabelLink', // invoked on link the resource with a target resource
'OnBabelUnlink' // invoked on unlink the resource from a target resource
'OnBabelUnlink', // invoked on unlink the resource from a target resource
'OnBabelTvsSynchronized' // invoked when TVs are synchronized
];

$success = true;
Expand Down
24 changes: 23 additions & 1 deletion core/components/babel/src/Babel.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,9 @@ public function synchronizeTvs($resourceId)
return;
}

/* Declare an array for TV changes during syncing. */
$tvChanges = [];

foreach ($syncTvs as $tvId) {
/* go through each TV which should be synchronized */
$tv = $this->modx->getObject('modTemplateVar', $tvId);
Expand All @@ -305,11 +308,30 @@ public function synchronizeTvs($resourceId)
/* don't synchronize resource with itself */
continue;
}
$tv->setValue($linkedResourceId, $tvValue);

$tvValueLinkedResource = $tv->getValue($linkedResourceId);

if ($tvValueLinkedResource !== $tvValue) {
$tv->setValue($linkedResourceId, $tvValue);

$tvChanges[] = [
'tvId' => $tvId,
'tvValue' => $tvValue,
'resourceId' => $linkedResourceId
];
}
}
$tv->save();
}

//When there are TV changes, trigger the OnBabelTvsSynchronized event
if (count($tvChanges) > 0) {
$this->modx->invokeEvent('OnBabelTvsSynchronized', [
'tvChanges' => $tvChanges,
'resourceId' => $resourceId
]);
}

$this->modx->cacheManager->refresh();
}

Expand Down