Changing form fields of Drupal - [#weight] in array is not enforced?

I have no experience in php. I followed several tutorials to modify my Drupal forms using the theme method in template.php .

For some reason, the [#weight] property for a field does not match its values. I would like to move the category field [cid] over the field [subject] .

Here are the lines of code I used:

 $form['cid']['#weight'] = 0.003; $form['subject']['#weight'] = 0.004; 

When I print my array for viewing, I see that the values โ€‹โ€‹have changed, but when I display the form, no changes occur. I already cleared the performance cache after each modification.

Here is a snippet of my print array:

 [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 ) 
+4
source share
1 answer

Usually you see that the behavior in node forms when CCK is activated. This is because CCK redefines the weight for the default fields and CCK with a custom ordering system by going to Content Management โ†’ Content Types โ†’ [Content Type] โ†’ Field Management.

Of course, if you disable CCK (maybe not an option), your #weight values โ€‹โ€‹will be respected.

It should be noted that although the Forms API allows decimal places for #weight , it is good practice to use integers.


Edit

If you want to work within the limits provided by CCK, in the user module you will need to implement the #pre_render handler, which will change the weight of the form elements after the CCK changes them:

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

The problem is that you donโ€™t know what CCK weight values โ€‹โ€‹were used for other form elements, therefore, while you order cid in front of the object, both fields can be displayed in the middle of all other fields on the page and not in the initial position.

CCK puts you in a corner when it comes to weight. The right way for Drupal / CCK to process orders for forms is to respect the choices made by the administrator in Content Management โ†’ Content Types โ†’ [Content Type] โ†’ Field Management.

If you have a custom form element, you can tell CCK so that it appears in the "Field Management" section by implementing hook_content_extra_fields() . Continuing the code above:

 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; } 
+9
source

All Articles