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

Php 修改数组中的Drupal表单字段-[#权重]不受尊重?,php,forms,drupal,field,Php,Forms,Drupal,Field,我没有php方面的经验。我已经学习了一些教程,使用template.php中的主题方法修改了Drupal表单 由于某些原因,字段的[#weight]属性不符合其值。我想将类别字段[cid]移到主题字段[Subject]上方 以下是我使用的代码行: $form['cid']['#weight'] = 0.003; $form['subject']['#weight'] = 0.004; 当我打印数组以查看时,我看到值已更改,但当我渲染表单时,不会进行任何更改。我已经在每次修改后清除了性能缓存 以

我没有php方面的经验。我已经学习了一些教程,使用
template.php
中的主题方法修改了Drupal表单

由于某些原因,字段的
[#weight]
属性不符合其值。我想将类别字段
[cid]
移到主题字段
[Subject]
上方

以下是我使用的代码行:

$form['cid']['#weight'] = 0.003;
$form['subject']['#weight'] = 0.004;
当我打印数组以查看时,我看到值已更改,但当我渲染表单时,不会进行任何更改。我已经在每次修改后清除了性能缓存

以下是我打印的数组的一个片段:

[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
        )

当启用CCK时,您通常会看到节点上的行为。这是因为CCK通过其自己的订购系统覆盖默认字段和CCK字段的权重,该订购系统可通过转到内容管理->内容类型->内容类型->管理字段进行配置

当然,如果禁用CCK(可能不是选项),您的
#weight
值将得到尊重

需要注意的一点是,虽然Forms API允许使用小数表示
#weight
,但使用整数是一种很好的做法


编辑

如果要在CCK呈现的约束范围内工作,则在自定义模块中,您需要实现一个
#preu 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;
}

问题是,您将不知道CCK用于其他表单元素的权重值,因此,虽然您在主题之前已经订购了CID,但是这两个字段可能出现在页面上的所有其他字段的中间,而不是在它们的原始位置。

当谈到重量时,CCK会迫使你陷入困境。Drupal/CCK处理表单排序的正确方法是尊重管理员在内容管理->内容类型->内容类型->管理字段上的选择

如果您有一个自定义表单元素,您可以通过实现
hook\u content\u extra\u fields()
将其告知CCK,使其显示在管理字段中。继续上面的代码:

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;
}

这是有道理的。有办法解决这个问题吗?我只需要一个例外的联系方式。我知道我可能会将其主题化,但是否有编码修复程序?@Chris,是和否。我已经用更多信息和一些可能的解决方案更新了我的答案。谢谢你的解决方案。我现在了解CCK如何处理表单字段。我想如果我有一个自定义表单,那么我会走这条路,但是因为我只需要在站点范围的联系人表单中重新排列一个[cid]字段,所以我将只使用CSS主题。我会记下你的解决方案,以备将来参考。非常感谢+答案是1。我建议阅读这篇关于
hook\u content\u extra\u fields()的文章。它也有助于理解CCK字段显示和重新排列的工作原理。