4 added 170 characters in body
source | link

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.

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);
  }
}

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.

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.

3 Found a better work around.
source | link

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);
  }
}

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.

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.

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.

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);
  }
}

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.

2 First answer didn't work as expected.
source | link

I thinkthought this can be done using hook_field_create_field. It would only work when you add a field to something, so existing fields would need to be modified in thebut it's not possible. Using that hook and then calling field_config table infield_update_field will not work because the database.update does not allow this:

function mymodule_field_create_field($field) {
  if ($field['type']$field['storage']['type'] ==!= 'addressfield'$prior_field['storage']['type']) {
    $field['storage']throw =new arrayFieldException(
        'type'"Cannot =>change 'my_custom_storage',
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.

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.

I think this can be done using hook_field_create_field. It would only work when you add a field to something, so existing fields would need to be modified in the field_config table in the database.

function mymodule_field_create_field($field) {
  if ($field['type'] == 'addressfield') {
    $field['storage'] = array(
        'type' => 'my_custom_storage',
    );
  }
}

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.

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.

1
source | link