Drupal:如何使用 CTools 使字段集相关

发布于 2024-09-06 00:36:12 字数 1139 浏览 5 评论 0原文

我正在使用 Ctools Dependency 使字段集可隐藏。这是我的代码的一部分:

$form['profile-status'] = array(
    '#type' => 'radios',
    '#title' => '',
    '#options' => array(
        'new' => t('Create a new profile.'),
        'select' => t('Use an existing profile.'),
    ),
);

$form['select'] = array(
    '#type' => 'select',
    '#title' => t('Select a profile'),
    '#options' => $options,
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('radio:profile-status' => array('select')),
);

$form['profile-properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('View the profile'),
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('radio:profile-status' => array('select')),
    '#input' => true,
);

在上面的代码片段中,有两个元素,一个选择和一个字段集。两者都有#process 和#dependency 参数,并且都指向一个依赖值字段。问题是像 select 或 textfield 这样的元素可以轻松隐藏,但它不适用于 fieldset。在支持请求页面中,CTools 创建者提到 '#input' => true 是一种解决方法。正如您所看到的,我将其添加到代码中,但效果不佳。

您有什么建议吗?

I am using Ctools Dependency to make a fieldset hideable. This is part of my code:

$form['profile-status'] = array(
    '#type' => 'radios',
    '#title' => '',
    '#options' => array(
        'new' => t('Create a new profile.'),
        'select' => t('Use an existing profile.'),
    ),
);

$form['select'] = array(
    '#type' => 'select',
    '#title' => t('Select a profile'),
    '#options' => $options,
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('radio:profile-status' => array('select')),
);

$form['profile-properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('View the profile'),
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('radio:profile-status' => array('select')),
    '#input' => true,
);

In snippet above, There are two elements, one select and one fieldset. Both have #process and #dependency parameters and both point to one field for dependent value. Problem is elements like select or textfield can be hidden easily but it does not work for fieldset. In this support request page, CTools creator has mentioned that '#input' => true is a work around. As you see I added it to code, but it does not work as well.

Do you have any suggestion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别忘他 2024-09-13 00:36:12

我在阅读了 CTools dependent 的源码后找到了答案。字段集应按如下方式更改:

$form['profile-properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('View the profile'),
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('radio:profile-status' => array('select')),
    '#input' => true,

    '#id' => 'my-fs-id',
    '#prefix' => '<div id="my-fs-id-wrapper">',
    '#suffix' => '</div>',
);

首先必须为字段集设置 ID。然后它必须包裹在 DIV 标签中。 DIV 的 ID 应该是 feildset 的 ID,后缀为“-wrapper”。

I found my answer after reading the source of CTools dependent. Fieldset should change as this:

$form['profile-properties'] = array(
    '#type' => 'fieldset',
    '#title' => t('View the profile'),
    '#process' => array('ctools_dependent_process'),
    '#dependency' => array('radio:profile-status' => array('select')),
    '#input' => true,

    '#id' => 'my-fs-id',
    '#prefix' => '<div id="my-fs-id-wrapper">',
    '#suffix' => '</div>',
);

First an ID must be set for he fieldset. Then it must be wrapped in a DIV tag. ID of the DIV should be feildset's ID suffixed with '-wrapper'.

天涯离梦残月幽梦 2024-09-13 00:36:12

现在(2013 年 2 月)用法是:

$form['foo_or_bar'] = array(
    '#title' => 'Foo or Bar',
    '#type' => 'radios',
    '#options' => array(
        "foo" => "Foo",
        "bar" => "Bar"
    ),
    '#default_value' => "foo",
);

$form['react_on_foo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Foo fieldset'),
    '#dependency' => array('radio:foo_or_bar' => array('foo')),
);

$form['react_on_foo']['dummy_for_foo_fieldset'] = array(
    '#title' => t('Dummy for FOO fieldset'),
    '#type' => 'textfield',
);


$form['react_on_bar'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bar fieldset'),
    '#dependency' => array('radio:foo_or_bar' => array('bar')),
);

$form['react_on_bar']['dummy_for_bar_fieldset'] = array(
    '#title' => t('Dummy for BAR fieldset'),
    '#type' => 'textfield',
);

并且不再需要#process。

now (Feb 2013) usage is:

$form['foo_or_bar'] = array(
    '#title' => 'Foo or Bar',
    '#type' => 'radios',
    '#options' => array(
        "foo" => "Foo",
        "bar" => "Bar"
    ),
    '#default_value' => "foo",
);

$form['react_on_foo'] = array(
    '#type' => 'fieldset',
    '#title' => t('Foo fieldset'),
    '#dependency' => array('radio:foo_or_bar' => array('foo')),
);

$form['react_on_foo']['dummy_for_foo_fieldset'] = array(
    '#title' => t('Dummy for FOO fieldset'),
    '#type' => 'textfield',
);


$form['react_on_bar'] = array(
    '#type' => 'fieldset',
    '#title' => t('Bar fieldset'),
    '#dependency' => array('radio:foo_or_bar' => array('bar')),
);

$form['react_on_bar']['dummy_for_bar_fieldset'] = array(
    '#title' => t('Dummy for BAR fieldset'),
    '#type' => 'textfield',
);

And #process is no more needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文