Automatic Change Detection

Automatic change detection consists of two parts:

  1. Data changed handler that handles a change a creates a changed item in the queue.
  2. Event listener (or custom business logic) that triggers the data changed handler.

Data Changed Handler

Data changed handler define how change of data should be handled in terms of creating change items in the translation queue. They define aspects like source and target languages, translation provider, checks if relevant data really changed and also if and what related elements should be considered too.

Data changed handler need to implement DataChangedHandlerInterface and might extend the abstract class DataChangedHandler. This bundle ships with a couple of actual implementation of change handlers for data objects, assets and documents.

To register these change handlers in the system, corresponding services have to be configured.

app.translations.data-changed-handler.object:   # Choose any service key
    autowire: true
    autoconfigure: true
    class:  Pimcore\TranslationsProviderInterfaceBundle\DataChangedHandler\DataObjectDataChangedHandler # May be a predefined handler or a custom handler implementation
    arguments:  # Required arguments (values depend on the API in use)
        $sourceLanguage: 'de'
        $targetLanguages: [en,fr]
        $translationsProvider: '@translations-provider.translationscom'  # The translations provider service (for the API in use)
        $allowedClasses: [News,Car]  # All allowed data object classes as array

There are no default data changed handler configured (since they have implementation specific settings). Therefore at least one changed handler has to be configured to set up automatic change detection.

Further customization

In order to add related elements to a job, the getRelatedElements method has to be overridden. Simply return an array containing all elements that are related to the object that was saved. This way the next time a job is created, the related elements will be within the same job as the saved element.

For example, a class Product has a href relation to a class ProductCategory. In case the ProductCategory is supposed to be translated as well, get the ProductCategory from the Product element (which is available in the getRelatedElements method). Return the ProductCategory within an array and everything is set up.

Event Listeners

If you want to enable automatic translation every time an object is saved, configure event listeners that trigger the registered data changed handlers. This bundle ships with a default implementation for such an event listener. To activate it, add following service definition:

services:
    Pimcore\TranslationsProviderInterfaceBundle\EventListener\DataChangedHandlerListener:
        autowire: true
        autoconfigure: true
        tags:
        - { name: kernel.event_listener, event: pimcore.dataobject.preUpdate, method: onPreUpdateDataObject, priority: -255 }
        - { name: kernel.event_listener, event: pimcore.asset.preUpdate, method: onPreUpdateAsset, priority: -255 }
        - { name: kernel.event_listener, event: pimcore.document.preUpdate, method: onPreUpdateDocument, priority: -255 }

Custom implementations of event listeners are possible too.