0

I've created a custom module for a field in drupal that has two fields, Position and Affiliation. I add the field to my test content, and set Num of Values to "Unlimited".

The Problem:

When I go to create a new instance of test content, instead of seeing one instance of my field and the "Add another item" button, it starts with two (see image) and the "Add another item" button.

If I set the Num of Values to 1, I only get one. If I click "add another item", it only adds one. I'm looking at my code, and for the life of me cannot figure out where the problem can be. I assume it's something in the widgit.

function pos_aff_select_field_widget_info() {
    return array(
        'pos_aff_select_widget_default' => array(
            'label' => t('Affiliation and Position default widget'),
            'description' => t('Drop Down Menu for Affiliation and Position'),
            'field types' => array('pos_aff_select'),
            'behaviors' => array(
                'multiple values' => FIELD_BEHAVIOR_DEFAULT,
                'default value' => FIELD_BEHAVIOR_DEFAULT,
            ),
        ),
    );
}

function pos_aff_select_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element){
    $base = $element;

    if($instance['widget']['type']  == 'pos_aff_select_widget_default') {
        $element['position'] = array(
            '#type' => 'select',
            '#title' => t('Position'),
            '#default_value' => isset($items[$delta]['position']) ? $items[$delta]['position'] : NULL,
            '#options' => pos_aff_select_query("ibs_position"),
        );

        $element['affiliation'] = array(
            '#type' => 'select',
            '#title' => t('Affiliation'),
            '#default_value' => isset($items[$delta]['affiliation']) ? $items[$delta]['affiliation'] : NULL,
            '#options' => pos_aff_select_query("ibs_affiliation"),
        );
    }
    return $element;
}

Does anyone have any clue what is going wrong?

1 Answer 1

0

I'll post my work around just in case anyone else has the same problem. But after fiddling around with it, I found out that if you have default values set, then it will set the first instance of the field to the default values and the second instance of the field to blank (I tested this by switching my select to textfield and if defaults were blank then I got only one instance of the field, if I put any text into the default, then I got two). Because my fields uses a select form then I, by default, had set my field values to something, thus, I had default values, thus, it showed the behavior I mentioned.

I set 'default value' => FIELD_BEHAVIOR_NONE, in the hook_field_widget_info and the field only produced one instance. Ridiculous.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.