修改Drupal表单字段 - 数组中的[#weight]不受尊重?

Chr*_*Lai 4 php forms drupal field

我没有PHP的经验.我已经按照一些教程使用template.php中的theme方法修改我的Drupal表单.

由于某种原因,字段的[#weight]属性不符合其值.我想在主题字段[subject]上方移动Category字段[cid].这些是我使用的代码行:

$form['cid']['#weight'] = 0.003;
$form['subject']['#weight'] = 0.004;
Run Code Online (Sandbox Code Playgroud)

当我打印我的数组进行查看时,我看到值已经改变,但是当我渲染表单时,没有进行任何更改.每次修改后我都已经清除了性能缓存.

如果您对此感兴趣,请阅读我的打印数据片段:

[subject] => Array
        (
            [#type] => textfield
            [#title] => Subject
            [#maxlength] => 255
            [#required] => 1
            [#post] => Array
                (
                )

            [#programmed] => 
            [#tree] => 
            [#parents] => Array
                (
                    [0] => subject
                )

            [#array_parents] => Array
                (
                    [0] => subject
                )

            [#weight] => 0.004
            [#processed] => 1
            [#description] => 
            [#attributes] => Array
                (
                )

            [#input] => 1
            [#size] => 60
            [#autocomplete_path] => 
            [#process] => Array
                (
                    [0] => form_expand_ahah
                )

            [#name] => subject
            [#id] => edit-subject
            [#value] => 
            [#defaults_loaded] => 1
            [#sorted] => 1
        )

    [cid] => Array
        (
            [#type] => select
            [#title] => Category
            [#default_value] => 1
            [#options] => Array
                (
                    [1] => General Enquiries
                    [2] => Support
                )

            [#required] => 1
            [#post] => Array
                (
                )

            [#programmed] => 
            [#tree] => 
            [#parents] => Array
                (
                    [0] => cid
                )

            [#array_parents] => Array
                (
                    [0] => cid
                )

            [#weight] => 0.003
            [#processed] => 1
            [#description] => 
            [#attributes] => Array
                (
                )

            [#input] => 1
            [#size] => 0
            [#multiple] => 
            [#process] => Array
                (
                    [0] => form_expand_ahah
                )

            [#name] => cid
            [#id] => edit-cid
            [#value] => 1
            [#defaults_loaded] => 1
            [#sorted] => 1
        )
Run Code Online (Sandbox Code Playgroud)

非常感激,

克里斯

小智 9

您通常会在启用CCK时看到节点上的行为.这是因为CCK通过转到内容管理 - > 内容类型 - > [内容类型] - > 管理字段来覆盖默认和CCK字段的权重,并且可以配置自己的订购系统.

当然,如果禁用CCK(可能不是选项),#weight则会尊重您的值.

需要注意的一点是,虽然Forms API允许使用小数#weight,但最好使用整数.


编辑

如果你想在CCK提出的约束内工作,在自定义模块中你需要实现一个#pre_render处理程序,它将在CCK改变它们之后改变表单元素的权重:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  $form['#pre_render'][] = 'mymodule_form_alter_weight';
}

function mymodule_form_alter_weight($elements) {
  $elements['cid']['#weight'] = 0.003;
  $elements['subject']['#weight'] = 0.004;

  return $elements;
}
Run Code Online (Sandbox Code Playgroud)

问题是,你不知道CCK用于其他表单元素的权重值是什么,所以当你在主题之前订购了cid时,这两个字段可能会显示在页面上所有其他字段的中间而不是他们原来的位置.

在重量方面,CCK迫使你进入角落.处理表单排序的正确Drupal/CCK方式是尊重管理员对内容管理 - > 内容类型 - > [内容类型] - > 管理字段所做出的选择.

如果您有一个自定义表单元素,您可以告诉CCK它,以便它通过实现显示在管理字段中hook_content_extra_fields().继续上面的代码:

function mymodule_content_extra_fields() {
  $extras['mymodule'] = array( // Name of field
    'label' => t('Mymodule'),
    'description' => t('Mymodule field'),
    'weight' => 10, // The default weight, can be overriden on Manage Fields
  );

  return $extras;
}
Run Code Online (Sandbox Code Playgroud)