如何向 Shopware 6 前端中的自定义 CMS 元素添加称呼下拉列表?

问题描述 投票:0回答:1

我开发了一个类似于联系表单的自定义 CMS 表单元素,但具有附加字段。但是,称呼字段在我的自定义表单中不显示选项。下面是我的元素在店面的视图:

{% block element_form %}
    {% set config = element.fieldConfig.elements %}
    
    <div class="cms-element-contact">
        <div class="row justify-content-center">
            <div class="col-lg-12">
    
                {% set formViolations = app.request.get('errors') %}
    
                <div class="card">
                    <div class="card-body">
                        <div class="card-title mb-4" v-if="element.config.title.value.length > 0">
                            {{ element.config.title.value }}
                        </div>
                        {% block cms_packageform %}
                        <form action="" method="post" data-form-validation="true">
    
                            <div class="form-content">
                                <div class="row g-2">
                                    {% block cms_packageform_select_salutation %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-select-salutation.html.twig'
                                                    with {
                                                    additionalClass: 'col-md-6',
                                                    required: true
                                                }
                                                %}
                                    {% endblock %}
    
                                    {% block cms_packageform_input_first_name %}
    
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    fieldName: 'firstName',
                                                    required: true,
                                                    additionalClass: 'col-md-6',
                                                    label: 'packageform.label.firstName',
                                                    placeholder: 'packageform.placeholder.firstName'
                                                }
                                                %}
                                    {% endblock %}
                                </div>
    
                                <div class="row g-2">
                                    {% block cms_packageform_input_email %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    fieldName: 'email',
                                                    type: 'email',
                                                    required: true,
                                                    additionalClass: 'col-md-6',
                                                    label: 'packageform.label.email',
                                                    placeholder: 'packageform.placeholder.email'
                                                }
                                                %}
                                    {% endblock %}
    
                                    {% block cms_packageform_input_phome %}
                                    {% set phoneNumberFieldRequired = config('core.basicInformation.phoneNumberFieldRequired') == true %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    fieldName: 'phone',
                                                    required: true,
                                                    additionalClass: 'col-md-6',
                                                    label: 'packageform.label.phone',
                                                    placeholder: 'packageform.placeholder.phone'
                                                }
                                                %}
                                    {% endblock %}
                                </div>
    
                                <div class="row g-2">
                                    {% block cms_packageform_input_street %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    required: true,
                                                    fieldName: 'street',
                                                    additionalClass: 'col-md-4',
                                                    label: 'packageform.label.street',
                                                    placeholder: 'packageform.placeholder.street'
                                                }
                                                %}
                                    {% endblock %}
                                    {% block cms_packageform_input_city %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    required: true,
                                                    fieldName: 'city',
                                                    additionalClass: 'col-md-4',
                                                    label: 'packageform.label.city',
                                                    placeholder: 'packageform.placeholder.city'
                                                }
                                                %}
                                    {% endblock %}
                                    {% block cms_packageform_input_zip %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    required: true,
                                                    fieldName: 'zip',
                                                    additionalClass: 'col-md-4',
                                                    label: 'packageform.label.zip',
                                                    placeholder: 'packageform.placeholder.zip'
                                                }
                                                %}
                                    {% endblock %}
                                </div>
                                <div class="row g-2">
                                    {% block cms_packageform_input_packagetype %}
                                        <div class="col-md-4">
                                            <label class="form-label" for="form-qty">{{ packageform.label.packagetype }}</label>
                                            <select  name="package['type']" id="packageform-packageType" required="required" class="form-select">
                                                    <option value="1">
                                                        Type 1
                                                    </option>
                                                    <option value="2">
                                                        Type 2
                                                    </option>
                                            </select>
                                        </div>
                                    {% endblock %}
                                    {% block cms_packageform_input_qty %}
                                    {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-input.html.twig'
                                                    with {
                                                    required: true,
                                                    fieldName: 'qty',
                                                    additionalClass: 'col-md-4',
                                                    label: 'packageform.label.quantity',
                                                    placeholder: 'Quantity',
                                                }
                                                %}
                                    {% endblock %}
                                    <div class="col-md-4">
                                        <button >Add Package</button>
                                    </div>
                                </div>
    
                                {% block cms_packageform_submit %}
                                {% sw_include '@Storefront/storefront/element/cms-element-form/form-components/cms-element-form-submit.html.twig' %}
                                {% endblock %}
                            </div>
    
                        </form>
                        {% endblock %}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endblock %}

请指导我如何将动态称呼字段添加到上面的表单中。

shopware shopware6
1个回答
1
投票

创建自定义 CMS 元素时,有时需要使用其他实体。在这些情况下,您可以实现自定义

CmsElementResolver
来解析配置数据。

  1. PackageFormCmsElementResolver.php
    目录中创建
    <plugin root>/src/Core/Content/Cms/DataResolver/Element/
    数据解析器。

文件内容

PackageFormCmsElementResolver.php
:

<?php declare(strict_types=1);

namespace Swag\BasicExample\Core\Content\Cms\DataResolver\Element;

use Shopware\Core\Content\Cms\DataResolver\Element\FormCmsElementResolver;

class PackageFormCmsElementResolver extends FormCmsElementResolver
{
    public function getType(): string
    {
        return 'package-form';
    }
}

我们的自定义解析器扩展自

FormCmsElementResolver
。正如您所看到的,
getType
方法反映了CMS元素的名称。

  1. <plugin root>/src/Resources/config/services.xml
    文件中注册自定义解析器。

文件内容

services.xml
:

<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

    <services>
        <service id="Swag\BasicExample\Core\Content\Cms\DataResolver\Element\PackageFormCmsElementResolver">
            <argument type="service" id="Shopware\Core\System\Salutation\SalesChannel\SalutationRoute"/>
            <tag name="shopware.cms.data_resolver"/>
        </service>
    </services>
</container>

称呼选择现在将在您的自定义 CMS 表单元素中可用。

更多信息,您可以参考文档。

© www.soinside.com 2019 - 2024. All rights reserved.