Addressfield Tips

Some RETS feeds separate the street address into many parts, however there is no way to really map them to the street portion of Addressfield, the following code snippet is a handy work around to this problem.

This uses the hook_drealty_import_presave_alter hook, it has two arguments:

  • $item: this is the entity that is about to be saved any changes to this entity will be persisted to the underlying storage mechanism.
  • $item_context: this is an array of contextual data from the import process and has the following keys.
    • field_mappings - an array of the current field mappings which is an array of the RETS systemnames keyed by field names
    • connection - the current drealty connection object [DrealtyConnection]
    • resource - the current drealty resource object [DrealtyRetsResource]
    • key_field - the key field for the current class
    • rets_item - the current RETS data that is being processed, data is keyed by the RETS SystemName
   function mymodule_drealty_import_presave_alter(&$item, $item_context) {
     // limit the function to only operate on listings (this could be improved, i'm using the bundle 'residential' to limit/filter on)
     if($item->type == 'residential') {
        // in this case my addressfield field is named field_address
        // the RETS feed provides the street address in two parts StreetNumber and StreetName 
        $item->field_address[LANGUAGE_NONE][0]['thoroughfare'] = $item_context['rets_item']['StreetNumber'] . ' ' . $item_context['rets_item']['StreetName'];
     }
   }

Comments

markusa’s picture

there's a typo in the code above.

$item->field_address[LANGUAGE_NONE[0]['thoroughfare'] = $item_context['rets_item']['StreetNumber'] . ' ' . $item_context['rets_item']['StreetName'];

should be
$item->field_address[LANGUAGE_NONE][0]['thoroughfare'] = $item_context['rets_item']['StreetNumber'] . ' ' . $item_context['rets_item']['StreetName'];

there's a closing bracket necessary after [LANGUAGE_NONE

Great bit of code, thanks for sharing!

camidoo’s picture

all fixed up! thanks for the heads up

markusa’s picture

Let's say you wish to alter a field's data other than the addressfield. For example in MLS RETS implementation I deal with the school types are given as simple one letter. For examply E stands for Elementary.

so for a field named field_school_type_1 you could alter it with code such as this

switch ($item->field_school_type_1[LANGUAGE_NONE][0]['value']){
case 'E':
case 'e':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'Elementary School';
		 break;
case 'M':
case 'm':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'Middle School';
		 break;
case 'I':
case 'i':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'Intermediate School';
              break;
case 'J':
case 'j':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'Junior High School';
		 break;
case 'H':
case 'h':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'High School';
               break;
case 'P':
case 'p':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'Primary School';
  	 break;
case 'S':
case 's':  $item->field_school_type_1[LANGUAGE_NONE][0]['value'] = 'Senior High School';
		 break;					 										 
}
VanLuda’s picture

I'm having trouble getting this working. Making a shot in the dark and hoping that someone could help me.

So I have the following fields:
Bundle name: MichRIC - Residential
Bundle machine name: michric_residential

Address field: field_address
RETS fields: The way I get them from the server is LIST_31, LIST_33, etc.

Here are my questions:

  1. Do I use the bundle name or bundle machine name for my IF statement? I'm assuming the machine name.
  2. For the 'rets_item', am I using the LIST_31 or the field name I created in Drupal or something else. No matter what I've tried, I can't get anything to populate in the address field on imports.

This is my first Drupal install so I'm trying to figure this out. I made a custom module and added the script from above. I'm not sure if this is the correct place to be adding this script. I figured it'd be better to add it in a custom module so I wouldn't have to worry about updates wiping my custom code.

VanLuda’s picture

Figured out my issue! I needed to begin the name of my function with the same as my module. Ugh, hours spent on something so silly.