Sylius:如何在网格渲染中注入它自己记录的数据变量

Sylius: How to inject data variable of it's own record in a grid rendering

我想根据该订单中的某些产品,在管理视图中为订单网格的渠道显示添加一些内容。在示例中,我需要一种方法将记录的数据注入到树枝模板中。

想不通。网格看起来像这样:

sylius_grid:
    grids:
        sylius_admin_order:
            driver:
                name: doctrine/orm
                options:
                    class: "%sylius.model.order.class%"
                    repository:
                        method: createListQueryBuilder
            sorting:
                number: desc
            fields:
                ...
                channel:
                    type: twig
                    label: sylius.ui.channel
                    sortable: channel.code
                    options:
                        template: "@SyliusAdmin/Order/Grid/Field/channel.html.twig"

我看到他们在其他列中添加了变量:在选项下:例如:

                    options:
                        template: ...
                        vars: 
                            labels: "@SyliusAdmin/Order/Label/State"

但是如何处理它自己,我的意思是处理记录中的其余数据? 我需要这样的东西:

                    options:
                        template: "@SyliusAdmin/Order/Grid/Field/channel.html.twig"
                        vars: 
                            order: self

有谁知道这样做的方法吗?

总之,您应该创建一个新模板并将其路径设置为网格字段选项-> 模板。然后在该模板中使用 {{ options.vars.order }} 获取变量。

工作原理:

Sylius-Grid 在渲染时将一个 options 变量传递给模板。例如,如何看起来标准 label 参数:

网格定义在@SyliusAdmin/config/grids/order.yml:

...
                paymentState:
                    type: twig
                    label: sylius.ui.payment_state
                    sortable: ~
                    options:
                        template: "@SyliusUi/Grid/Field/state.html.twig"
                        vars:
                            labels: 
...

@SyliusUi/Grid/Field/label.html.twig:

{% set value = 'sylius.ui.' ~ data %}

{% if options.vars.labels is defined %}
    {% include [(options.vars.labels ~ '/' ~ data ~ '.html.twig'), '@SyliusUi/Label/_default.html.twig'] with {'value': value} %}
{% else %}
    {% include '@SyliusUi/Label/_default.html.twig' with {'value': value} %}
{% endif %}

在此示例中,传递的变量 labels 在 Grid Yaml 定义中传递,在 Twig options.vars.labels 变量中可用。

编辑:如果你想访问模板中实体对象的其他属性,而不只是像本例中的channels,将其添加到你的网格字段定义根:

                channel:
                    type: twig
                    label: sylius.ui.channel
                    path: .

您可能需要使用 php bin/console cache:clear 刷新 Twig 缓存。在 https://docs.sylius.com/en/1.6/components_and_bundles/bundles/SyliusGridBundle/field_types.html#twig-twig

中阅读更多相关信息