Tag: Drupal

Drupal 7 Field API – ordering form widgets

March 16th, 2011 — 2:10am

I was very eager to start using Drupal 7. I was aware that many changes have been made to the core system, but my first task definitely proved to be a challenge. Currently, documentation and online help is sparse at best. I know that this will change over time. So, I’ll stick with D6 for the coming months.

Here’s the result of my first D7 challenge. Maybe I can save you some time.

The Scenario

I created a custom content type with a module. In Drupal 7 you can attach fields to forms in your .install file. So, I tried it. I added a taxonomy field to my content type.

The Problem

The taxonomy field appeared at the end of the ‘node edit’ form. I wanted it to appear just after the title field, and I couldn’t figure out how to set a weight on the field. (Ordering fields on the ‘view node’ page was a little easier and documented to an extent.)

The Solution

  $field_instance =  array(
    'some_field' => array(
       'field_name' => 'my_category',
       'entity_type' => 'node',
       'label' => $t('Category'),
       'bundle' => 'my_bindle',
       'required' => FALSE,
       'widget' => array(
          'type' => 'options_select',
          'weight' => -2,
        'display' => array('default'=>array('weight'=>-3)),

Notice that I set the weight in 2 places. The weight in the widget definition is the one that moved the field on the node edit form.

Hope this helps someone.

Comment » | Uncategorized

Back to top