【问题标题】:OroPlatform: override oro_datetime_widget optionsOroPlatform:覆盖 oro_datetime_widget 选项
【发布时间】:2021-06-15 15:03:18
【问题描述】:

上下文

我实际上是在尝试更改OroDateTimeType::class 的时间输入的默认占位符。

例如,我想要文本 Horaires 而不是 Temps

这是我的 FormType 中的表单字段:

->add('expirationDate', OroDateTimeType::class, [
    'label' => 'app.subscription.fields.expirationDate',
])

在我的树枝视图中:

form_row(form.expirationDate)

问题

一开始,我尝试将 Symfony 4 placeholder 选项用于 DateTime 类型:https://symfony.com/doc/4.4/reference/forms/types/date.html#placeholder。它不起作用,因为OroDateTime 使用了不同的日期选择器,并且它在加载时覆盖了 Symfony 值:

{# vendor/oro/platform/src/Oro/Bundle/FormBundle/Resources/views/Form/fields.html.twig #}

{% block oro_datetime_widget %}
    {% set dateValidation = {'Date' : {}} %}
    {% set timeValidation = {'Time' : {}} %}

    {% if required %}
        {% set dateValidation = dateValidation|merge({'NotBlank' : {}}) %}
        {% set timeValidation = timeValidation|merge({'NotBlank' : {}}) %}
    {% endif %}

    {% if attribute(attr, 'class') is defined %}
        {% set attrClass = attr['class'] %}
    {% else %}
        {% set attrClass = '' %}
    {% endif %}

    {% set options = {
        view: 'oroui/js/app/views/datepicker/datetimepicker-view',
        nativeMode: isMobileVersion(),
        dateInputAttrs: {
            placeholder: 'oro.form.choose_date'|trans,
            id: id,
            name: id,
            class: 'input-small datepicker-input ' ~ attrClass,
            'data-validation': dateValidation|json_encode(constant('JSON_FORCE_OBJECT')),
            'aria-live': 'assertive',
            autocomplete: 'off',
            autocorrect: 'off',
            autocapitalize: 'off'
        },
        datePickerOptions: {
            altFormat: 'yy-mm-dd',
            changeMonth: true,
            changeYear: true,
            yearRange: years|default('-80:+1'),
            showButtonPanel: true
        },
        timeInputAttrs: {
            placeholder: 'oro.form.choose_time'|trans,
            id: 'time_selector_' ~ id,
            name: 'time_selector_' ~ id,
            class: 'input-small timepicker-input ' ~ attrClass,
            'data-validation': timeValidation|json_encode(constant('JSON_FORCE_OBJECT'))
        },
        timePickerOptions: {
        }
    } %}
    {% set id = 'hidden_' ~ id %}
    {% set attr = attr|merge({
        'data-page-component-module': 'oroui/js/app/components/view-component',
        'data-page-component-options': options|json_encode(constant('JSON_FORCE_OBJECT'))
    }) %}

    {{ block('datetime_widget') }}
{% endblock oro_datetime_widget %}

如果我从 options 变量中更改值 timeInputAttrs.placeholder。有用。 但是,我想将此变量传递给我的特定表单字段,而不是全局传递。

更新

我最终选择在全局更改我的项目中的oro.form.choose_time 翻译。

所以,在我的Resources/translations/messages.fr_FR.yml 中,我创建了这些行:

oro:
  form:
    choose_time: Horaires
  auth:
    description:
      main: Baltimore

然后,我了解到翻译是在位于var/cache/dev/translations/catalogue.fr_FR 的文件中生成的:

<?php

use Symfony\Component\Translation\MessageCatalogue;

$catalogue = new MessageCatalogue('fr_FR', array (
  'messages' => 
  array (
    'oro.form.choose_time' => 'Temps',
    'oro.auth.description.main' => 'Baltimore',

在这里,我可以看到 oro.auth.description.main 更改已应用,但键 oro.form.choose_time 的值仍然相同。

也许我有一个命令要运行?

【问题讨论】:

  • 当您已经使用来自 Crowdin 的更新或 UI 中的手动编辑覆盖翻译时,将不再应用 YAML 更改。在这种情况下,您必须编写数据迁移来更新数据库中加载的翻译。

标签: symfony orocrm orocommerce


【解决方案1】:

在 Oro 应用程序 UI 中覆盖任何文本的最简单方法是覆盖用于呈现它的消息的翻译。由于表单占位符也被翻译,您可以使用此技术。如果这是您对表单唯一需要的自定义,请关注this guide

如果你想覆盖一个 HTML,你可以通过 the template overriding guide 来扩展模板。

但是,由于您想修改单个表单的标签,那么最好的方法是 extend the form type 并使用新选项覆盖单个表单字段。

【讨论】:

  • 感谢不同的方法。我已经用你的第一个解决方案更新了我问题的最后一部分。
猜你喜欢
  • 2021-06-04
  • 2021-09-28
  • 2021-05-23
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多