Commit 74e6ff20 authored by Drew Webber's avatar Drew Webber
Browse files

Issue #3212398 by jake_milburn: [D7] Field API assumes serial/integer entity...

Issue #3212398 by jake_milburn: [D7] Field API assumes serial/integer entity IDs, but the entity system does not
parent d6e292ba
Loading
Loading
Loading
Loading
+12 −0
Original line number Diff line number Diff line
@@ -212,6 +212,18 @@ function _field_sql_storage_schema($field) {
    ),
  );

  // If the target entity type uses a string for its entity ID then update
  // the fields entity_id and revision_id columns from INT to VARCHAR.
  if (!empty($field['entity_id_type']) && $field['entity_id_type'] === 'string') {
    $current['fields']['entity_id']['type'] = 'varchar';
    $current['fields']['entity_id']['length'] = 128;
    unset($current['fields']['entity_id']['unsigned']);

    $current['fields']['revision_id']['type'] = 'varchar';
    $current['fields']['revision_id']['length'] = 128;
    unset($current['fields']['revision_id']['unsigned']);
  }

  $field += array('columns' => array(), 'indexes' => array(), 'foreign keys' => array());
  // Add field columns.
  foreach ($field['columns'] as $column_name => $attributes) {
+23 −0
Original line number Diff line number Diff line
@@ -104,6 +104,29 @@ class FieldSqlStorageTestCase extends DrupalWebTestCase {
    $this->assertFalse(array_key_exists($unavailable_language, $entity->{$this->field_name}), 'Field translation in an unavailable language ignored');
  }

  /**
   * Tests adding a field with an entity ID type of string.
   */
  function testFieldSqlSchemaForEntityWithStringIdentifier() {
    // Test programmatically adding field with string ID.
    $field_name = 'string_id_example';
    $field = array('field_name' => $field_name, 'type' => 'text', 'settings' => array('max_length' => 255), 'entity_id_type' => 'string');
    field_create_field($field);
    $schema = drupal_get_schema('field_data_' . $field_name);

    $this->assertEqual($schema['fields']['entity_id']['type'], 'varchar');
    $this->assertEqual($schema['fields']['revision_id']['type'], 'varchar');

    // Test programmatically adding field with default ID(int).
    $field_name = 'default_id_example';
    $field = array('field_name' => $field_name, 'type' => 'text', 'settings' => array('max_length' => 255));
    field_create_field($field);
    $schema = drupal_get_schema('field_data_' . $field_name);

    $this->assertEqual($schema['fields']['entity_id']['type'], 'int');
    $this->assertEqual($schema['fields']['revision_id']['type'], 'int');
  }

  /**
   * Reads mysql to verify correct data is
   * written when using insert and update.
+9 −0
Original line number Diff line number Diff line
@@ -795,6 +795,14 @@ function field_ui_field_overview_form_submit($form, &$form_state) {

  $destinations = array();

  // Check if the target entity uses a non numeric ID.
  $entity_info = entity_get_info($entity_type);
  if (!empty($entity_info['entity_id_type']) && $entity_info['entity_id_type'] === 'string') {
    $entity_id_type = 'string';
  } else {
    $entity_id_type = NULL;
  }

  // Create new field.
  $field = array();
  if (!empty($form_values['_add_new_field']['field_name'])) {
@@ -804,6 +812,7 @@ function field_ui_field_overview_form_submit($form, &$form_state) {
      'field_name' => $values['field_name'],
      'type' => $values['type'],
      'translatable' => $values['translatable'],
      'entity_id_type' => $entity_id_type,
    );
    $instance = array(
      'field_name' => $field['field_name'],