0

I have a custom compound field working beautifully. Below is what I have defined in the hook_field_schema function:

function MODULE_field_schema(&$columns, &$indexes, &$keys) {
  $columns = array(
    'meat' => array(
      'type'        => 'int',
      'description' => 'Meat'
    ),
    'potatoes' => array(
      'type'        => 'int',
      'description' => 'Potatoes',
    ),
  );
}

Now I need to modify the schema like so:

function MODULE_field_schema(&$columns, &$indexes, &$keys) {
  $columns = array(
    'meat' => array(
      'type'        => 'int',
      'description' => 'Meat'
    ),
    'potatoes' => array(
      'type'        => 'int',
      'description' => 'Potatoes',
    ),
    'carrots' => array(
      'type'        => 'int',
      'description' => 'Carrots'
    ),
  );
}

The problem is that I am using this field on a few content types and I need a way to update those associated tables. Basically I need to know what hook I need to create to make this happen.

2

I completed this in hook_update_n:

function MODULE_update_7201(&$sandbox) {

  // get all field names
  $field_names = array();
  $fields = field_info_instances('node');
  foreach ($fields as $node) {
    foreach ($node as $node_field => $field) {
      $field_names[$node_field] = TRUE;
    }
  }

  // load schema to update
  $columns = array();
  MODULE_field_schema($columns);

  // load all fields and check for type
  foreach ($field_names as $field_name => $junk) {

    // load field info
    $field = field_info_field($field_name);
    if ($field['type'] == FIELD_NAME) {

      // loop schema and check fields for existance
      foreach ($columns as $db_field_name => $db_field_schema) {
        if (!db_field_exists('field_data_' . $field_name, $field_name . '_' . $db_field_name)) {
          db_add_field('field_data_' . $field_name, $field_name . '_' . $db_field_name, $db_field_schema);
        }
        if (!db_field_exists('field_revision_' . $field_name, $field_name . '_' . $db_field_name)) {
          db_add_field('field_revision_' . $field_name, $field_name . '_' . $db_field_name, $db_field_schema);
        }
      }
    }
  }
}
| improve this answer | |

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.