Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
there's a typo in the code
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!
Mark Hanna
My Blog
mark@skvare.com
all fixed up! thanks for the
all fixed up! thanks for the heads up
Let's say you wish to alter other fields
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
Mark Hanna
My Blog
mark@skvare.com
I'm having trouble getting this working
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:
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.
Figured out my issue! I
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.