Community Documentation

Tips and Tricks

Last updated August 15, 2012. Created by camidoo on July 28, 2012.
Log in to edit this page.

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

<?php
  
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!

all fixed up! thanks for the

all fixed up! thanks for the heads up

camidoo

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

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;
}

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 7.x
Audience
Designers/themers, Programmers, Site administrators, Site builders
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here