【EC-CUBE4.3】プラグインを開発してみる その３

## 本記事で分かること
- プラグインでの通知メールテンプレートのカスタマイズの仕方

## 手順
### 1. 前回記事と同様の手順で、お問い合わせページをカスタマイズ
#### 1. プラグイン用の Twig ファイルを作成
- `app/Plugin/OrderInquiry/Resource/template/default/Contact/index.twig`
```php
{% 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`
```php
{% 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 クラスに追加
```php
    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 を拡張する
```php
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](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3850968/f4227779-bcfc-4682-96bf-4efddfd6bdd2.png)

- お問い合わせページに、ご注文番号の項目を追加できた。