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】プラグインを開発してみる その3

Last updated at Posted at 2025-04-21

本記事で分かること

  • プラグインでの通知メールテンプレートのカスタマイズの仕方

手順

1. 前回記事と同様の手順で、お問い合わせページをカスタマイズ

1. プラグイン用の Twig ファイルを作成

  • app/Plugin/OrderInquiry/Resource/template/default/Contact/index.twig
{% block script %}
<script>
    $(function () {
        $('#order_inquiry_form_order_no').insertAfter($('div.ec-borderedDefs dl').eq(4));
    });
</script>
{% endblock %}
{% block main %}
<dl id="order_inquiry_form_order_no">
    <dt>
        {{ form_label(form.order_no, 'order_inquiry.common.order_number', { 'label_attr': { 'class': 'ec-label' }}) }}
    </dt>
    <dd>
        <div class="ec-input{{ has_errors(form.order_no, form.order_no) ? ' error' }}">
            {{ form_widget(form.order_no) }}
            {{ form_errors(form.order_no) }}
        </div>
    </dd>
</dl>
{% endblock %}
  • app/Plugin/OrderInquiry/Resource/template/default/Contact/confirm.twig
{% block script %}
<script>
    $(function () {
        $('#order_inquiry_form_order_no').insertAfter($('div.ec-borderedDefs dl').eq(4));
    });
</script>
{% endblock %}
{% block main %}
<dl id="order_inquiry_form_order_no">
    <dt>
        {{ form_label(form.order_no, 'order_inquiry.common.order_number', { 'label_attr': { 'class': 'ec-label' }}) }}
    </dt>
    <dd>
        {{ form.order_no.vars.data|nl2br }} 
        {{ form_widget(form.order_no, { type : 'hidden' }) }}
    </dd>
</dl>
{% endblock %}

2. Event クラスに追加

    public static function getSubscribedEvents(): array
    {
        return [
            'Mypage/history.twig' => 'onHistoryDetail',
            'Contact/index.twig' => 'onContactIndex',       //追加 
            'Contact/confirm.twig' => 'onContactConfirm',   //追加
        ];
    }

    public function onHistoryDetail(TemplateEvent $event): void
    {
        $twig = '@OrderInquiry/default/Mypage/history.twig';
        $event->addSnippet($twig);
    }

    // 追加
    public function onContactIndex(TemplateEvent $event): void
    {
        $twig = '@OrderInquiry/default/Contact/index.twig';
        $event->addSnippet($twig);
    }

    // 追加
    public function onContactConfirm(TemplateEvent $event): void
    {
        $twig = '@OrderInquiry/default/Contact/confirm.twig';
        $event->addSnippet($twig);
    }

2. ContactType を拡張する

namespace Plugin\OrderInquiry\Form\Extension\Type\Front;

use Eccube\Common\EccubeConfig;
use Eccube\Form\Type\Front\ContactType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints as Assert;

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

    /**
     * ContactTypeExtension constructor.
     *
     * @param EccubeConfig $eccubeConfig
     */
    public function __construct(EccubeConfig $eccubeConfig)
    {
        $this->eccubeConfig = $eccubeConfig;
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'order_no',
            TextType::class,
            [
                'required' => false,
                'constraints' => [
                    new Assert\Length(['max' => $this->eccubeConfig['eccube_stext_len']]),
                ],
                'attr' => [
                    'placeholder' => 'order_inquiry.common.order_number.sample',
                ],
            ]
        );
    }

    /**
     * {@inheritdoc}
     */
    public function getExtendedType()
    {
        return ContactType::class;
    }

    /**
     * Return the class of the type being extended.
     */
    public static function getExtendedTypes(): iterable
    {
        return [ContactType::class];
    }
}

3. ブラウザで表示を確認して反映をチェックする

image.png

  • お問い合わせページに、ご注文番号の項目を追加できた。
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?