1

How can I alter a field's storage property? What's the correct hook to use to alter $field['storage'] = 'my_custom_storage';?

I am currently using the global db variable field_storage_default but I find that to be gross. I don't want to do it for all fields.

3

I thought this can be done using hook_field_create_field, but it's not possible. Using that hook and then calling field_update_field will not work because the update does not allow this:

  if ($field['storage']['type'] != $prior_field['storage']['type']) {
    throw new FieldException("Cannot change an existing field's storage type.");
  }

Drupal doesn't want you to screw up existing data. So being careful and knowing what you are doing, the existing fields could be modified in the field_config table in the database. Reimplementing field_update_field you could do:

/**
* Implements hook_field_create_field().
*/
function mymodule_field_create_field($field) {
  if ($field['type'] == 'fieldtypetostore') {
    $field['storage'] = array(
        'type' => 'mymodule_custom_storage',
        'settings' => array(),
        'module' => 'mymodule',
        'active' => 1,
    );

    // Build record to update.
    $record = array(
      'id' => $field['id'],
      'storage_type' => $field['storage']['type'],
      'storage_module' => $field['storage']['module'],
      'storage_active' => $field['storage']['active'],
    );

    // Update the field storage.
    $primary_key = array('id');
    drupal_write_record('field_config', $record, $primary_key);
  }
}

/**
* Implements hook_field_update_field().
*/
function mymodule_field_update_field($field) {
  mymodule_field_create_field($field);
}

The best way to do this is to create the fields in your module that you need to use and at that time, set the storage for the field. It's cleaner than changing existing fields.

0

I am using this hook_field_storage_info_alter() to alter the storage field settings.

If yours executes last of all the modules, then you can change any storage field settings here.

  • That's not what I am looking for. I have a custom field storage defined. I want to be able to use that storage for an individual field. Currently I have to set the database global which tells ALL fields to use my storage instead of field_sql_storage. I want to be able to do this on a per field basis. – thepearson Feb 28 '12 at 1:00

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.