How to extend the plugin entity from the customization area of ​​EC-CUBE4

Hostnbit

Many of the open source EC -CUBE plug -ins allow user customization.

You can customize the plugin and use it if you feel like “ this plugin doesn’t have enough functionality or you want to change it.” However, if you customize it, you will have to keep up with version updates each time, making it difficult.

This time, we will show you how to easily update the version even if you customize the plug-in .

In fact, plug-in entities can also be extended with traits from the customization area.

This is also something that is not mentioned in the official documentation *1 , but a plugin ‘s Entity canapp/Customize/Entity be extended by creating a Trait.

Create a trait in app/Customize/Entity

For example , if you want to add a field to the Manufacturer Plugin’s Entity, app/Customize/Entity/MakerTrait.phpcreate it and make it look like the following.

<?php
namespace Customize\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation as Eccube;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Eccube\EntityExtension("Plugin\Maker4\Entity\Maker")
 */

trait MakerTrait
 {

    /**
     * @var String|null
     *
     * @ORM\Column(name="contents", type="text", nullable=true)
     * @Eccube\FormAppend(
     *     auto_render=true,
     *     type="\Symfony\Component\Form\Extension\Core\Type\TextareaType",
     *     options={
     *          "required": false,
     *          "label": "メーカーコンテンツ",
     *     })
     */
    private $contents; 

    /**
     * @param string
     * @return Eccube\Entity\Category
     */
    public function setContents($contents)
    {
        $this->contents = $contents;
        return $this;
    }

    /**
     * @return \DateTime
     */
    public function getContents()
    {
        return $this->contents;
    }  

}

Add a check to see if it has been defined to the plugin Entity

If this continues, the name of the generated proxy and the name of the plugin Entity will conflict, so it cannot be used.

Add a check to see if the Entity of the plugin has been defined (whether there is a proxy).

<?php

/*
 * This file is part of EC-CUBE
 *
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
 *
 * http://www.lockon.co.jp/
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Plugin\Maker4\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Entity\AbstractEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Mapping\ClassMetadata;

if (!class_exists('\Plugin\Maker4\Entity\Maker')) {// <-これ
    /**
     * Class Maker.
     *
     * @ORM\Table(name="plg_maker")
     * @ORM\Entity(repositoryClass="Plugin\Maker4\Repository\MakerRepository")
     */
    class Maker extends AbstractEntity
    {
    ...
    }
}

Request to all EC-CUBE4 plug-in creators!

I would like to include the code for this predefined check in advance when creating a plugin , as I would like to avoid modifying the plugin code as much as possible! !

Generate proxy and update DB structure

Then, generate a proxy with a console command to reflect the schema changes.

$ ./bin/console eccube:generate:proxies
$ ./bin/console eccube:schema:update --force

Then, app/proxy/entity/app/Plugin/Maker4/Entity/Maker.phpit will be generated and the column will be added to the DB table.

Add the created field to the twig template on the management screen

If this is the case, the column will just be added, so let’s add it to the template.

app/Plugin/Maker4/Resource/template/admin/maker.twigThe area where the form for the manufacturer’s name is displayed

{# Entity extension automatic output #}
{% for f in form if f.vars.eccube_form_options.auto_render %}
    {% if f.vars.eccube_form_options.form_theme %}
        {% form_theme f f.vars.eccube_form_options.form_theme %}
        {{ form_row(f) }}
    {% else %}
        <div class="form-row mb-3">
            <div class="col-3">
                <span>{{ f.vars.label|trans }}</span>
            </div>
            <div class="col">
                {{ form_widget(f) }}
                {{ form_errors(f) }}
            </div>
        </div>
    {% endif %}
{% endfor %}

If you clear the cache and reload the relevant screen, the added field will be displayed.

Hostnbit

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Menu Title
Scroll to Top