是否可以将所选选项的标签动态设置为转发器字段的标签?

Is it possible to dynamically set the label of a selected option as the label for a repeater field?

Elementor 具有转发器字段类型,\Elementor\Controls_Manager::REPEATER。文档 (https://developers.elementor.com/elementor-controls/repeater-control/) 说字段的标签可以在 title_field 参数中定义:

'title_field' => '{{{ list_title }}}'

这很好,如果该字段是常规文本字段。但是只要字段类型是 selectable 的东西,例如 \Elementor\Controls_Manager::SELECT{{{ field_name }}} 东西(不管它叫什么)都会利用 selected 选项的值。这当然是合乎逻辑的,但不切实际,因为选项值通常是在编程上下文中可用的东西,人类阅读起来不是很愉快。

有没有办法改用标签?通过标签,如果是 select 元素,我指的是 selected option 标签的内部文本。

这可能不是最复杂的解决方案,但它确实有效。可以通过在 <##> 括号之间添加 javascript 代码来操纵密钥。

// Define the labels
$labels = [
    'foo' => __('Foo'),
    'bar' => __('Bar'),
];
    
// Convert to JSON
$labels_json = json_encode($labels);

// $this refers to the widget object; context here is the _register_controls() method
$this->add_control('meta_keys', [
    'label' => __('Meta keys'),
    'type' => \Elementor\Controls_Manager::REPEATER,
    'title_field' => "<# "
        . "let labels = $labels_json; " // Now the labels are available to the javascript
        . "let label = labels[key]; "
        . "#>"
        . "{{{ label }}}",
    'fields' => [
        [
            'name' => 'key',
            'label' => __('Meta key'),
            'type' => \Elementor\Controls_Manager::SELECT,
            'options' => $labels,
            'placeholder' => __('Meta key'),
            'default' => __('Meta key'),
        ]
    ],
]);