0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【EC-CUBE4.3】product テーブルにカラムを追加する その2

Last updated at Posted at 2025-04-15

目的

製品(dtb_product)にもフリガナを登録できるようにしたいとのこと。

やること

管理画面 商品登録・詳細に「商品名(カナ)」を表示する。

手順

1. 商品登録・詳細のtwigファイルを編集する。

前回、dtb_product テーブルに、kana カラムを追加した。

今回、商品名の下に、商品名(カナ)を表示したい。

  1. app/Customize/template/admin/Product/product.twigを作成
  2. src/Eccube/Resource/template/admin/Product/product.twigをコピーして1で作ったproduct.twigにペースト
  3. 以降は、app/Customize配下のproduct.twigを編集する
  4. 商品名の記述の下に、追記
<div class="row">
    <div class="col-3">
        <div class="d-inline-block">
            <span>商品名カナ</span>
            <span class="badge bg-primary ms-1">
                {{ 'admin.common.required'|trans }}
            </span>
        </div>
    </div>
    <div class="col mb-2">
        {{ form_widget(form.kana) }}
        {{ form_errors(form.kana) }}
    </div>
</div>

2. ProductType.php を拡張して、kana フィールドを追加する

app/Customize/Form/Type/Admin/Extension/ProductTypeExtension.phpを作成する。

namespace Customize\Form\Type\Admin\Extension;

use Eccube\Form\Type\Admin\ProductType;
use Eccube\Common\EccubeConfig;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\BirthdayType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;


class ProductTypeExtension extends AbstractTypeExtension
{
    /**
     * @var EccubeConfig
     */
    protected $eccubeConfig;

    /**
     * ProductType constructor.
     *
     * @param EccubeConfig $eccubeConfig
     */

    public function __construct(
        EccubeConfig $eccubeConfig
    ) {
        $this->eccubeConfig = $eccubeConfig;
    }

    // どのフォームを拡張するかを宣言(EC-CUBEのバージョンによって記述方法が異なる)
    public static function getExtendedTypes(): iterable
    {
        yield ProductType::class; // ProductTypeを拡張
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // 追加したいフィールドを記述
        // TextTypeで文字列を入力するフィールドを指定
        // 「'required' => false」で入力必須ではないことを指定
        $builder
            ->add('kana', TextType::class, [
                'required' => false,
                'constraints' => [
                    // new Assert\NotBlank(),
                    new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
                ],
            ]);
    }
}

3. 試しに商品編集から登録してみる

問題なく登録することができた。
image.png

追記

必須なのに、

`required` => false,

していたりするので、コピペする場合は調整して下し亜。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?