7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

EC-CUBE4で超簡単なプラグインを作る

Last updated at Posted at 2019-10-24

やること

./bin/console eccube:plugin:generateで骨組みを作る
プロダクトテーブルにニックネームカラムを追加する
管理画面でニックネームの編集を行える様にする

やらないこと

公開用画面にニックネームを表示する

YouTobeに実際の手順動画をアップロードしました

githubに出来上がったプラグインファイルを公開しました

generateを実行

EC-CUBE4をインストールしたディレクト直下で以下のコマンドを実行

./bin/console eccube:plugin:generate

code, nameに Foo7を指定、verに1.0.0を指定

EC-CUBE Plugin Generator Interactive Wizard
\===========================================

 name [EC-CUBE Sample Plugin]:
 \> Foo7

 code [Sample]:
 \> Foo7

 ver [1.0.0]:
 \> 

                                                                                                                        
 [OK] Plugin was successfully created: Foo7 Foo7 1.0.0                                                                  
                                                                                                                        

app/Plugin/Foo7ディレクト以下に骨組みが出来る

💾 tree app/Plugin/Foo7
app/Plugin/Foo7
├── Controller
│   └── Admin
│       └── ConfigController.php
├── Entity
│   └── Config.php
├── Event.php
├── Form
│   ├── Extension
│   └── Type
│       └── Admin
│           └── ConfigType.php
├── Nav.php
├── Repository
│   └── ConfigRepository.php
├── Resource
│   ├── doctrine
│   ├── locale
│   │   ├── messages.ja.yaml
│   │   └── validators.ja.yaml
│   └── template
│       └── admin
│           └── config.twig
├── TwigBlock.php
└── composer.json

config関連は使わないので削除する。

find . -type f  | grep config  ← Plugin/Foo7以下でファイル名にconfigが含まれるファイル一覧を取得するshell script.
rm -f `find . -type f  | grep config`  ← ファイル一覧スクリプトを `(チルダ)で囲むことで ファイル削除コマンドにファイル一覧を渡す。名前にconfigが含まれるファイルを全部消す。(別にconfigが含まれるファイルを一個づつ消しても問題ないです。)

プロダクトテーブルにニックネームカラム追加定義ファイルを作成
テキストエディタでEntity/Foo7ProductTrait.phpを開いて以下のコード入力。

<?php
namespace Plugin\Foo7\Entity;

use Doctrine\ORM\Mapping as ORM;
use Eccube\Annotation as Eccube;

/**
 * @Eccube\EntityExtension("Eccube\Entity\Product")
 */
trait Foo7ProductTrait
{
    /**
     * @var string
     *
     * @ORM\Column(name="nickname", type="string", length=32, nullable=true)
     */
    private $nickname;  // ← 追加するカラム名

    /**
     * @return string
     */
    public function getNickname() // ← 追加したカラムのゲッター
    {
        return $this->nickname;
    }

    /**
     * @param string $nickname
     */
    public function setNickname($nickname)  // ← 追加したカラムのセッター
    {
        $this->nickname = $nickname;
    }

}

管理画面にニックネーム編集欄を表示する定義を作成
テキストエディタで Entity/Foo7ProductTrait.php を開いて以下のコードを入力。

<?php
  
namespace Plugin\Foo7\Form\Extension;

use Eccube\Form\Type\Admin\ProductType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints as Assert;

class Foo7AdminProd extends AbstractTypeExtension
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder  // フォームビルだに追加(Symfonyの機能みたい。まだ深く理解してない。)
            ->add('nickname', TextType::class, [
                'label' => 'ニックネーム',
                'required' => false,
                'attr' => [
                    'maxlength' => 32,
                    'placeholder' => 'アイスマン',
                ],
                'eccube_form_options' => [
                    'auto_render' => true,
                ],
                'constraints' => [
                    new Assert\Length(['max' => 32]),
                ],
        ]);
    }

    public function getExtendedType()
    {
        return ProductType::class;
    }

}

./bin/console eccube:plugin:install --code=Foo7
を実行してプラグインをインストール

./bin/console eccube:plugin:enable --code=Foo7
を実行してプラグインを有効化

管理画面で商品登録画面を開くとニックネーム欄が現れる。

prod_edit.png
7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?