From a02794e2b3157a2ab8947195512a559c6e2b70a8 Mon Sep 17 00:00:00 2001
From: Artem Kolotilkin <artem.kolotilkin@gmail.com>
Date: Mon, 7 Dec 2015 00:45:24 -0500
Subject: [PATCH] Add Addressfield preprocessor

---
 includes/processor.inc              |  4 ++--
 includes/processor_addressfield.inc | 44 +++++++++++++++++++++++++++++++++++++
 search_api.info                     |  1 +
 search_api.module                   | 10 +++++++++
 4 files changed, 57 insertions(+), 2 deletions(-)
 create mode 100644 includes/processor_addressfield.inc

diff --git a/includes/processor.inc b/includes/processor.inc
index fe92882..e4bbc57 100644
--- a/includes/processor.inc
+++ b/includes/processor.inc
@@ -396,7 +396,7 @@ abstract class SearchApiAbstractProcessor implements SearchApiProcessorInterface
    *   The field's information.
    *
    * @return
-   *   TRUE, iff the field should be processed.
+   *   TRUE, if the field should be processed.
    */
   protected function testField($name, array $field) {
     if (empty($this->options['fields'])) {
@@ -407,7 +407,7 @@ abstract class SearchApiAbstractProcessor implements SearchApiProcessorInterface
 
   /**
    * @return
-   *   TRUE, iff the type should be processed.
+   *   TRUE, if the type should be processed.
    */
   protected function testType($type) {
     return search_api_is_text_type($type, array('text', 'tokens'));
diff --git a/includes/processor_addressfield.inc b/includes/processor_addressfield.inc
new file mode 100644
index 0000000..7641d4c
--- /dev/null
+++ b/includes/processor_addressfield.inc
@@ -0,0 +1,44 @@
+<?php
+
+/**
+ * @file
+ * Contains SearchApiAddressfield.
+ */
+
+/**
+ * Processor for appending full text value of administrative_area (state)
+ * to its abbreviation (i.e., "CA" => "CA California").
+ */
+class SearchApiAddressfield extends SearchApiAbstractProcessor {
+
+  public function preprocessIndexItems(array &$items) {
+    foreach ($items as &$item) {
+      foreach ($item as $name => &$field) {
+        // Don't preprocess the field with an empty value
+        if (!empty($field['value'])) {
+
+          $field_name = explode(":", $name);
+          $field_info = field_read_fields(array('field_name' => $field_name[0]));
+          // Only process fields of addressfield type
+          if (!empty($field_info) && $field_info[$field_name[0]]['type'] == "addressfield") {
+
+            // Process administrative_area (State) sub-field
+            if (isset($field_name[1]) && $field_name[1] == "administrative_area") {
+
+              // Get country code
+              $country_code = reset(explode(" ", $item[$field_name[0] . ':country']['value']));
+              module_load_include('inc', 'addressfield', 'addressfield.administrative_areas');
+              $administrative_areas = addressfield_get_administrative_areas($country_code);
+
+              if (!empty($administrative_areas)) {
+                // Append administrative_area full value to abbreviation
+                $field['value'] .= " " . $administrative_areas[$field['value']];
+                $this->processField($field['value'], $field['type']);
+              }
+            }
+          }
+        }
+      }
+    }
+  }
+}
\ No newline at end of file
diff --git a/search_api.info b/search_api.info
index 2c4ba14..d30c80e 100644
--- a/search_api.info
+++ b/search_api.info
@@ -24,6 +24,7 @@ files[] = includes/datasource_multiple.inc
 files[] = includes/exception.inc
 files[] = includes/index_entity.inc
 files[] = includes/processor.inc
+files[] = includes/processor_addressfield.inc
 files[] = includes/processor_highlight.inc
 files[] = includes/processor_html_filter.inc
 files[] = includes/processor_ignore_case.inc
diff --git a/search_api.module b/search_api.module
index b099741..a0cc1c5 100644
--- a/search_api.module
+++ b/search_api.module
@@ -1123,6 +1123,16 @@ function search_api_search_api_processor_info() {
     'class' => 'SearchApiHighlight',
     'weight' => 35,
   );
+  if (module_exists('addressfield')) {
+    $processors['search_api_addressfield'] = array(
+      'name' => t('Address field'),
+      'description' => t('This processor appends full text value of administrative_area (state) to its abbreviation ' .
+          '(i.e., "CA" => "CA California"). '),
+      'class' => 'SearchApiAddressfield',
+      'weight' => 40,
+    );
+  }
+
 
   return $processors;
 }
-- 
2.2.1

