Skip to main content

Shopware.model.Container - Using translations

Category: Shopware
shopware-logo

It is quite comfortable to make use of "Shopware.model.Container" for detail views, because record entities will be associated automatically. But by default translations won't be available. Here is the workaround:

Ext.define('Shopware.apps.MyPlugin.view.detail.Plugin', {
    extend: 'Shopware.model.Container',
    defaults: {
        //width: 400
    },
    alias: 'widget.Plugin-detail-view',
    initComponent: function () {
        var me = this;

        if (me.plugins === undefined) {
            me.plugins = [];
        }

        me.plugins.push(
            {
                pluginId: 'translation',
                ptype: 'translation',
                translationType: 'plugin-translation'
            }
        );

        me.callParent(arguments);
    },
    getForm: function () {
        return this;
    },

    getRecord: function () {
        return this.record;
    },

    getFields: function () {
        var me = this;

        return {
            items: [
                me.down('[name=field1]'),
                me.down('[name=field2]')
            ]
        };
    },
    configure: function() {
        var me = this;

        return {
            controller: 'MyController',
            fieldSets:
            [
                {
                    title: 'General',
                    defaults: { anchor: '100%' },
                    layout: 'anchor',
                    fields: {
                        field1: {
                            fieldLabel: 'Field 1',
                            xtype: 'textfield',
                            allowBlank: true,
                            translatable: true,
                            translationName: 'field1'
                        },
...

Translations will be stored in table "s_core_translations". You will need to load them manually.

$shopId = Shopware()->Shop()->getId();

$repo = $em->getRepository('Shopware\Models\Translation\Translation');
$translation = $repo->findOneBy(['shopId' => $shopId, 'key' => $elementId, 'type' => $type]);

//$translation->getData();

"objecttype" has been defined as "plugin-translation" before. "objectkey" equals the record id. "objectlanguage" corresponds to target shop id. 

if(!translation)
{
  $fallbackId = Shopware()->Shop()->getFallback() ? Shopware()->Shop()->getFallback()->getId() : null;

  //... get translation for fallback if fallback is set
}