PHP code example of momocode / shopware-5-plugin-base

1. Go to this page and download the library: Download momocode/shopware-5-plugin-base library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

momocode / shopware-5-plugin-base example snippets




namespace MyPlugin;

use Momocode\ShopwareBase\Plugin;

// Autload extra dependencies
if (file_exists(__DIR__ . '/vendor/autoload.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;
    }
}




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.
    }
}


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

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

if (version_compare('1.0.1', $oldPluginVersion, '>')) {
    $this->removeField('field_name');
}



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/');
        }
    }
}
xml
<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>  
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}
xml
<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>