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

Idea: Fire custom event when TV values have been synchronized #201

Closed
arjen-t opened this issue Mar 15, 2024 · 2 comments
Closed

Idea: Fire custom event when TV values have been synchronized #201

arjen-t opened this issue Mar 15, 2024 · 2 comments

Comments

@arjen-t
Copy link
Contributor

arjen-t commented Mar 15, 2024

It would be nice if a custom event is triggered with the TV IDs in combination with the resource IDs that have changed after syncing the TV values.

Why?
This allows other Extras, that do something with this TV value for a specific resource, to still hook into the changes.

Suggested event name:
BabelTvsSynchronized

@Jako
Copy link
Collaborator

Jako commented Mar 15, 2024

Can you use this event?

$this->modx->invokeEvent('OnBabelLink', [
'context_key' => $this->getProperty('context'),
'original_id' => $this->object->get('id'),
'original_resource' => &$this->object,
'target_id' => $this->targetResource->get('id'),
'target_resource' => &$this->targetResource
]);

If not, you have to create a PR.

@arjen-t
Copy link
Contributor Author

arjen-t commented Mar 15, 2024

unfortunately not, since when updating a resource the TV's synchronization also has to be tracked. I will create a PR as suggested.

public function synchronizeTvs($resourceId)
{
$linkedResources = $this->getLinkedResources($resourceId);
/* check if Babel TV has been initiated for the specified resource */
if (empty($linkedResources)) {
$linkedResources = $this->initBabelTvById($resourceId);
}
/* synchronize the TVs of linked resources */
$syncTvs = $this->getOption('syncTvs');
if (empty($syncTvs) || !is_array($syncTvs)) {
/* there are no TVs to synchronize */
return;
}
foreach ($syncTvs as $tvId) {
/* go through each TV which should be synchronized */
$tv = $this->modx->getObject('modTemplateVar', $tvId);
if (!$tv) {
continue;
}
$tvValue = $tv->getValue($resourceId);
foreach ($linkedResources as $linkedResourceId) {
/* go through each linked resource */
if ($resourceId == $linkedResourceId) {
/* don't synchronize resource with itself */
continue;
}
$tv->setValue($linkedResourceId, $tvValue);
}
$tv->save();
}
$this->modx->cacheManager->refresh();
}

I assume this is the best place to trigger that event? Which will cause 2 scenarios.

  1. If the resource gets linked, the next events will be fired in the following order: OnBabelTvsSynchronized and then event OnBabelLink.
  2. If the resource gets updated/saved, the event OnBabelTvsSynchronized gets fired.

I will call the event OnBabelTvsSynchronized which is inline with the OnBabelLink convention.

The event OnBabelTvsSynchronized will be only triggered when their are changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants