momocode / shopware-5-plugin-base

作为Shopware 5插件中的composer依赖项加载的基础库

1.1.1 2021-02-01 14:26 UTC

This package is auto-updated.

Last update: 2024-09-29 05:42:29 UTC


README

此库包含可能在所有自定义插件中都有用的抽象。它提供以下功能:

  • 插件安装和更新时的数据库迁移
    • Shopware配置迁移(即将推出)
    • 自定义属性迁移
    • 自定义模型迁移(即将推出)

安装

首先在您的插件中要求composer包

composer require momocode/shopware-5-plugin-base

将composer自动加载器添加到插件引导类中,并让您的插件继承抽象

<?php

namespace MyPlugin;

use Momocode\ShopwareBase\Plugin;

// Autload extra dependencies
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
    require_once __DIR__ . '/vendor/autoload.php';
}

class MyPlugin extends Plugin {}

迁移

要执行插件安装和更新时的迁移,您可以从插件基础文件中受益,该文件在安装或更新时自动加载迁移文件。要使用它们,您必须将您的迁移文件放置在插件中的Migration文件夹中。

属性迁移

以下是一个继承自插件Migration/Attribute文件夹中的AbstractAttributeMigration的属性迁移文件示例。重要的是文件和类的名称中必须包含后缀"Migration"。插件基础类将找到此迁移文件,并在插件安装时创建所需的所有属性,在插件更新时仅创建或更新新版本的属性。

<?php

namespace MomoMailjet\Migration\Attribute;

use Momocode\ShopwareBase\Migration\Attribute\AbstractAttributeMigration;
use Shopware\Bundle\AttributeBundle\Service\TypeMapping;

class UserMigration extends AbstractAttributeMigration
{
    /**
     * Get table name
     *
     * @return string
     */
    public function getTableName()
    {
        return 's_user_attributes';
    }

    /**
     * Get column prefix
     *
     * @return string
     */
    protected function getColumnPrefix()
    {
        return 'momo_mailjet';
    }

    /**
     * Get default options for fields
     *
     * @return array
     */
    protected function getDefaultOptions()
    {
        return [
            'displayInBackend' => true,
            'custom' => false,
            'position' => $this->getPosition(),
        ];
    }

    /**
     * Get attribute fields for this migration
     *
     * @return array
     */
    protected function getFields()
    {
        $fields = [];

        $fields[] = [
            'email',
            TypeMapping::TYPE_STRING,
            [
                'label' => 'Mailjet E-Mail',
            ],
            '1.0.0',
        ];

        return $fields;
    }
}

情感小部件

此库为情感小部件提供抽象。它封装了安装和更新例程。在插件基础文件中,如果小部件位于Widgets文件夹中,并且文件和类名以Widget结尾,则小部件会自动安装和更新。以下是在您的插件Widgets文件夹中的小部件类示例

<?php

namespace MomoMailjet\Widgets;

use Momocode\ShopwareBase\Widget\AbstractWidget;

class MailjetBuiltInWidget extends AbstractWidget
{
    /**
     * Name of the plugin the widget belongs to
     *
     * @return string
     */
    public function getPluginName()
    {
        return 'MomoMailjet';
    }

    /**
     * Name of the widget
     *
     * @return string
     */
    public function getWidgetName()
    {
        return 'Mailjet: Eingebautes Widget';
    }

    /**
     * XType to listen for
     *
     * @return string
     */
    public function getXType()
    {
        return 'emotion-components-mailjet-built-in-widget';
    }

    /**
     * Register javascript for the widget
     *
     * @return string
     */
    protected function getBackendJsPath()
    {
        return 'backend/emotion/mailjet_built_in_widget/bootstrap.js';
    }

    /**
     * Widget options
     *
     * @return array
     */
    public function getWidgetOptions()
    {
        return [
            'template' => 'mailjet_built_in_widget', // must be created in widgets/emotion/components/
            'cls' => 'emotion-mailjet-built-in-widget', // additional css class
            'description' => 'Built-In Anmeldeformular', // Backend description
        ];

    }

    /**
     * List of fields
     *
     * @return array
     */
    public function getWidgetFields()
    {
        $fields = [];

        $fields[] = [
            'xtype' => 'textfield',
            'name' => 'text',
            'fieldLabel' => 'Simple text field',
            'position' => 10,
            'allowBlank' => true,
        ];

        return $fields;
    }

    /**
     * Update widget on plugin update
     *
     * @param string $oldPluginVersion
     *
     * @throws \Exception
     */
    public function updateWidget($oldPluginVersion)
    {
        // TODO: Implement updateWidget() method.
    }

    /**
     * Enrich assign data array with data from the widget
     *
     * @param array $data
     *
     * @return array
     */
    protected function getTemplateData($data)
    {
        // TODO: Implement getTemplateData() method.
    }
}

getWidgetFields()函数中定义的域将被安装 ... 在插件更新时调用updateWidget()函数,在那里您可以根据新插件版本执行这些操作

添加新字段

将您的字段添加到getWidgetFields()函数中,并将其添加到updateWidget()函数中,如果您想在新版本1.0.1中添加字段

if (version_compare('1.0.1', $oldPluginVersion, '>')) {
    $this->addField('new_field_name');
}
更新字段

getWidgetFields()函数中更新您的字段,并将其添加到updateWidget()函数中,如果您想在新版本1.0.1中更新字段

if (version_compare('1.0.1', $oldPluginVersion, '>')) {
    $this->updateField('updated_field_name');
}
删除字段

getWidgetFields()函数中删除您的字段,并将其添加到updateWidget()函数中,如果您想在新版本1.0.1中删除字段

if (version_compare('1.0.1', $oldPluginVersion, '>')) {
    $this->removeField('field_name');
}
模板数据
服务定义

我们需要创建服务定义,并将其标记为订阅者,以便小部件可以注册后端模板并在请求时转换前端模板数据。

<service id="momo_mailjet.widgets.mailjet_built_in_widget"
         class="MomoMailjet\Widgets\MailjetBuiltInWidget"
         parent="momocode.shopware_base.widget.abstract_widget">
    <tag name="shopware.event_subscriber"/>
</service>  

现在您需要在您的插件类中定义的路径中创建这三个文件

bootstrap.js
//{block name="backend/emotion/view/detail/elements/base"}
//{$smarty.block.parent}
//{include file='backend/emotion/mailjet_built_in_widget/Emotion.view.components.MailjetBuiltInWidget.js'}
//{include file='backend/emotion/mailjet_built_in_widget/Emotion.view.detail.elements.MailjetBuiltInWidget.js'}
//{/block}

将其他两个文件添加到后端模板中。

Emotion.view.components.MailjetBuiltInWidget.js
//{block name="emotion_components/backend/mailjet_built_in_widget"}
Ext.define('Shopware.apps.Emotion.view.components.MailjetBuiltInWidget', {
    extend: 'Shopware.apps.Emotion.view.components.Base',
    alias: 'widget.emotion-components-mailjet-built-in-widget'
});
//{/block}

在别名中,widget.后的部分必须与您在getXType()函数中定义的相同。

Emotion.view.detail.elements.MailjetBuiltInWidget.js
//{namespace name=backend/plugins/momomailjet/emotion}
Ext.define('Shopware.apps.Emotion.view.detail.elements.MailjetBuiltInWidget', {
    extend: 'Shopware.apps.Emotion.view.detail.elements.Base',
    alias: 'widget.detail-element-emotion-components-mailjet-built-in-widget',
    icon: 'data:image/png;base64,iVBORw0KGg...',
    compCls: 'emotion--mailjet-built-in-widget'
});

在别名中,widget.detail-element-后的部分必须与您在getXType()函数中定义的相同。

除此之外,您还需要在前端模板文件中为您的小工具创建一个文件,无论是在您的主题中还是在您的插件视图目录中。它必须放置在 widgets/emotion/components 文件夹中,并且命名为您在 getWidgetOptions() 函数的 template 属性中定义的名称。以下是一个示例

mailjet_built_in_widget.tpl

至少您需要一个订阅者来注册您的插件视图路径

<?php

namespace MomoMailjet\Subscriber;

use Enlight\Event\SubscriberInterface;
use Enlight_Template_Manager;

class TemplateSubscriber implements SubscriberInterface
{
    /**
     * @var Enlight_Template_Manager
     */
    protected $templateManager;

    /**
     * @var string
     */
    protected $pluginBaseDirectory;

    /**
     * @param Enlight_Template_Manager $templateManager
     * @param string $pluginBaseDirectory
     */
    public function __construct(Enlight_Template_Manager $templateManager, $pluginBaseDirectory)
    {
        $this->templateManager = $templateManager;
        $this->pluginBaseDirectory = $pluginBaseDirectory;
    }

    /**
     * Use the early "Enlight_Controller_Action_PreDispatch" event to register the template directory of the plugin.
     *
     * @inheritdoc
     */
    public static function getSubscribedEvents()
    {
        return [
            'Enlight_Controller_Action_PreDispatch' => 'onPreDispatch',
            'Enlight_Controller_Action_PostDispatchSecure_Backend_Emotion' => 'onPostDispatchBackendEmotion',
        ];
    }

    /**
     * On pre dispatch
     *
     * @param \Enlight_Controller_ActionEventArgs $args
     */
    public function onPreDispatch(\Enlight_Controller_ActionEventArgs $args)
    {
        $this->templateManager->addTemplateDir($this->pluginBaseDirectory . '/Resources/views');
    }

    /**
     * Register templates for custom designer components
     *
     * @param \Enlight_Controller_ActionEventArgs $args
     */
    public function onPostDispatchBackendEmotion(\Enlight_Controller_ActionEventArgs $args)
    {
        $controller = $args->getSubject();
        $view = $controller->View();

        if ($view) {
            $view->addTemplateDir($this->pluginBaseDirectory . '/Resources/views/');
        }
    }
}
<service id="momo_mailjet.subscriber.template_subscriber" class="MomoMailjet\Subscriber\TemplateSubscriber">
    <argument type="service" id="template" />
    <argument>%momo_mailjet.plugin_dir%</argument>
    <tag name="shopware.event_subscriber"/>
</service>