2

I'm trying to save multiple values from multi select option form which I created in the custom widget with:

$element['labs'] = array(
  '#type' => 'select',
  '#options' => $list,
  '#multiple' => 'multiple',
  '#title' => t('Labs'),
);

The biggest problem is the values won't save! As I know the delta should increment automatically if cardinality is set to more than 1.

1) I can't change cardinality or some other setting after activating the field in the edit field form

2) I can't see my custom field in the field type list to select from if I try to alter existing widgets:

'default_widget' => 'options_select',
'default_formatter' => 'status_field_options',

How could I extend standard list field type to populate the multiselect with my own generated data?

OR

How could I write my widget that Drupal let me save multiple values with different delta?

1

The values won't save when you don't have any storage to write into.

Make sure your widget implements hook_field_schema() and provide necessary schema for a field structure in your .install file. E.g.

/**
 * Implements hook_field_schema().
 */
function MYMODULE_field_schema($field) {
  switch ($field['type']) {
    default:
      $columns = array(
        'value' => array(
          'type' => 'text',
          'not null' => FALSE,
        ),
      );
      break;
  }
  return array(
    'columns' => $columns,
    'indexes' => array(
      'value' => array('value'),
    ),
  );
}

Then in hook_field_info() make sure you've FIELD_BEHAVIOR_DEFAULT for multiple values behavior, e.g.

/**
 * Implements hook_field_widget_info().
 */
function MYMODULE_field_field_widget_info() {
  return array(
    'my_widget' => array(
      'label' => t('Default'),
      'field types' => array('select'),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

When implementing multiple field properties as part of one widget (such as address field), please check: How to override default cardinality for the field widget?

For complete example, check existing modules under field core module or check field_example module (part of Examples).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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