I created a module that adds multicolumn display to checkboxes/radios form elements: http://drupal.org/project/multicolumncheckboxesradios

I'm trying to add this option to any CCK field that uses the checkboxes/radios widget so that user the set CCK widget checkboxes/radios to display in columns. Ideally I would like the multicolumn settings added to the "Global settings" of a field. But it seems there is no way to "extend" fields to have additional global_settings value.

I'm looking at the source content.crud.inc:

function _content_field_write($field, $op = 'update') {
  // Rearrange the data to create the global_settings array.
  $field['global_settings'] = array();
  $setting_names = module_invoke($field['module'], 'field_settings', 'save', $field);
  if (is_array($setting_names)) {
    foreach ($setting_names as $setting) {
      // Unlike _content_field_instance_write() and 'widget_settings', 'global_settings'
      // is never preexisting, so we take no particular precautions here.
      $field['global_settings'][$setting] = isset($field[$setting]) ? $field[$setting] : '';
      unset($field[$setting]);
    }
  }
  // 'columns' is a reserved word in MySQL4, so our column is named 'db_columns'.
  $field['db_columns'] = $field['columns'];

  switch ($op) {
    case 'create':
      drupal_write_record(content_field_tablename(), $field);
      break;
    case 'update':
      drupal_write_record(content_field_tablename(), $field, 'field_name');
      break;
  }
  unset($field['db_columns']);
  return $field;
}

The line

$setting_names = module_invoke($field['module'], 'field_settings', 'save', $field);

determines what fields get saved. It would be great if other module can add to this list.

Likewise, in content_field_edit_form() of content.admin.inc,

the line

$additions = module_invoke($field_type['module'], 'field_settings', 'form', $field);

gives the form elements to set global settings. It would be great if other module can add elements here.

Right now there is a solution: #508990: Integration with cck fields?

it basically completely overriding optionwidgets and adding 'widget_settings':

function multicolumncheckboxesradios_widget_info() {
  return optionwidgets_widget_info();
}

function multicolumncheckboxesradios_widget(&$form, &$form_state, $field, $items, $delta = NULL) {
  return optionwidgets_widget($form, $form_state, $field, $items, $delta);
}

function multicolumncheckboxesradios_widget_settings($op, $widget) {
  return my stuff here...
}

Is there better way to do this?

Comments

markus_petrux’s picture

Status: Active » Closed (duplicate)

Here's my suggestion:

a) Use hook_form_alter() against the field settings form to add your options to $form['field']['myoptions'].

b) In your form submit handler, just use something like variable_set('yourmodule_fieldname_option', $form_state['values']['myoptions']) to store the value of your options.

c) Implement hook_content_fieldapi() to catch up the event when a field is removed so that you can clean your vars.

d) Implement hook_uninstall() to clean your vars when the module is uninstalled.

I'm marking your issue as dup because of this: #417122: Allow drupal_alter() on Field and Widget Settings. But that's not *in*, hence my suggestion here.