From 51e8a114f9c1673e7c2df470693c3b15376376b2 Mon Sep 17 00:00:00 2001
From: Artem Kolotilkin <artem.kolotilkin@gmail.com>
Date: Tue, 15 Dec 2015 23:50:58 -0500
Subject: [PATCH] Issue #1269608: Integrate Search API with Addressfield

---
 addressfield.info                   |  1 +
 addressfield.module                 | 14 +++++++++
 includes/processor_addressfield.inc | 59 +++++++++++++++++++++++++++++++++++++
 3 files changed, 74 insertions(+)
 create mode 100644 includes/processor_addressfield.inc

diff --git a/addressfield.info b/addressfield.info
index af5d068..001e9af 100644
--- a/addressfield.info
+++ b/addressfield.info
@@ -6,6 +6,7 @@ package = Fields
 dependencies[] = ctools
 
 files[] = addressfield.migrate.inc
+files[] = includes/processor_addressfield.inc
 files[] = views/addressfield_views_handler_field_administrative_area.inc
 files[] = views/addressfield_views_handler_field_country.inc
 files[] = views/addressfield_views_handler_filter_country.inc
diff --git a/addressfield.module b/addressfield.module
index 68b45d9..e825166 100644
--- a/addressfield.module
+++ b/addressfield.module
@@ -906,3 +906,17 @@ function _addressfield_country_options_list($field = NULL, $instance = NULL) {
 
   return $countries;
 }
+
+/**
+ * Implements hook_search_api_processor_info().
+ */
+function addressfield_search_api_processor_info() {
+  $callbacks['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',
+  );
+
+  return $callbacks;
+}
diff --git a/includes/processor_addressfield.inc b/includes/processor_addressfield.inc
new file mode 100644
index 0000000..4810b14
--- /dev/null
+++ b/includes/processor_addressfield.inc
@@ -0,0 +1,59 @@
+<?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 configurationForm() {
+    $form = parent::configurationForm();
+    $form += array(
+      'address_field' => array(
+        '#markup' => '<p>There are no configurations for Address field processor in its current version,
+                    as it only appends full text value of administrative_area (state) to its abbreviation
+                    (i.e., "CA" => "CA California").</p>
+                    <p>Make sure that both fields are added to index (administrative_area and country).</p>',
+      ),
+    );
+    unset($form['fields']);
+
+    return $form;
+  }
+
+  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
-- 
2.6.2

