0

I am trying to add a field (middle name) into the field_schema of the contrib addressfield module (I initially didn't want to hack it, but was unsucessful in adding a field in from my custom module.). The module has the following hook_field_schema()

function addressfield_field_schema() {
  $columns = array(
    'country' => array(
      'description' => 'Two letter ISO country code of this address.',
      'type' => 'varchar',
      'length' => 2,
      'not null' => FALSE,
      'default' => '',
    ),
    'administrative_area' => array(
      'description' => 'The administrative area of this address. (i.e. State/Province)',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'sub_administrative_area' => array(
      'description' => 'The sub administrative area of this address.',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'locality' => array(
      'description' => 'The locality of this address. (i.e. City)',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'dependent_locality' => array(
      'description' => 'The dependent locality of this address.',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'postal_code' => array(
      'description' => 'The postal code of this address.',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'thoroughfare' => array(
      'description' => 'The thoroughfare of this address. (i.e. Street address)',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'premise' => array(
      'description' => 'The premise of this address. (i.e. Apartment / Suite number)',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'sub_premise' => array(
      'description' => 'The sub_premise of this address.',
      'type' => 'varchar',
      'length' => 255,
      'default' => '',
      'not null' => FALSE,
    ),
    'organisation_name' => array(
      'description' => 'Contents of a primary OrganisationName element in the xNL XML.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'name_line' => array(
      'description' => 'Contents of a primary NameLine element in the xNL XML.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'first_name' => array(
      'description' => 'Contents of the FirstName element of a primary PersonName element in the xNL XML.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'last_name' => array(
      'description' => 'Contents of the LastName element of a primary PersonName element in the xNL XML.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'last_name' => array(
      'description' => 'Contents of the LastName element of a primary PersonName element in the xNL XML.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => FALSE,
      'default' => '',
    ),
    'data' => array(
      'description' => 'Additional data for this address.',
      'type' => 'text',
      'size' => 'big',
      'not null' => FALSE,
      'serialize' => TRUE,
    ),
  );

  return array(
    'columns' => $columns,
    // TODO Add indexes.
  );
}

I found this post online for doing a field_schema update https://drupal.org/node/150215, but I don't know what the field name or table name is, its not listed in the file. How can I do this? My ultimate goal is to make a standalone plugin file to be used by addressfield, but I needed to update the schema first.

1

The field will be named whatever you named it when you added it. If you can't remember, go to the "Manage fields" UI page for the relevant entity type/bundle and you'll be able to find it there.

The table names will be based on that field name; if the field is called field_my_field then the two database tables are:

  • field_data_field_my_field
  • field_revision_field_my_field
  • oh, so in my case, I am using drupal commerce and it created an addressfield with the machine name commerce_customer_address, and the table name field_data_commerce_customer_address, and I do see all those columns there. So when I do this update, its only for that instance of the field? it won't exist for other addressfields (ie: I have another named membership), unless I do the update on both field instances? – user1015214 Nov 7 '13 at 16:44
  • No, stop!! Fields are global; instances of those fields are not. It's not possible to have a type of field which has different columns depending on what it's attached to. That would be the use case for a new field type altogether. Your DRY sensibilities might be alarmed at that, but that's how things are in Drupal 7 – Clive Nov 7 '13 at 16:50
  • Sorry, thats not what I meant. I meant that the addressfield module creates a field type 'postal_address'. I am creating different instances of that field type. Would there still be the same columns? – user1015214 Nov 7 '13 at 17:00
  • Yes - the columns that make up a field are global. So if you add a new column, every entity/bundle that attaches that field type will potentially (important word there) have that extra column. However, I guess you can control the widget form based on the entity/bundle combination, and just not render the input control for that column when it's not appropriate. – Clive Nov 7 '13 at 17:41
  • So, when using fieldname in db_add_field, I could really use any instance of the field_type 'postal_address' and it would do the same thing? – user1015214 Nov 7 '13 at 18:28

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.