From e53423345a863a2b5b63027be9092c0f3d05bba7 Mon Sep 17 00:00:00 2001
From: Jakob Torp <jakob@revealit.dk>
Date: Tue, 26 Jul 2011 12:46:35 +0200
Subject: [PATCH] Issue #1229676 by googletorp: Add
 hook_addressfield_handlers_alter

---
 addressfield.api.php |   32 ++++++++++++++++++++++++++++++++
 addressfield.module  |   20 ++++++++++++++++++++
 2 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/addressfield.api.php b/addressfield.api.php
index 3880fd9..f9bfe55 100644
--- a/addressfield.api.php
+++ b/addressfield.api.php
@@ -21,9 +21,41 @@
  *       this field is being rendered in.
  *     - (optional) 'delta': when generated for a field, the delta of the
  *       currently handled address.
+ *     - (optional) 'entity': The entity that the form/display is created for
+ *     - (optional) 'entity_type': The entity_type of the entity provided
+ *     - (optional) 'entity_types': If an entity_type can't be established,
+ *       an array of types is passed instead.
  *
  * @ingroup addressfield_format
  */
 function CALLBACK_addressfield_format_callback(&$format, $address, $context = array()) {
   
 }
+
+/**
+ * Allow other modules to alter at run time which handlers to use.
+ * Useful when you want to conditionally add/remove handlers based on data
+ * stored on an entity.
+ *
+ * @param &$handlers
+ *    Array of handlers that can be used. Use a FALSE value to indicate it
+ *    shouldn't be used and the name to indicate it should be used to generate
+ *    the address.
+ * @param $address
+ *    The address data used for the form
+ * @param $context
+ *   An array of context arguments:
+ *     - 'mode': can be either 'form' or 'render'
+ *     - (optional) 'field': when generated for a field, the field
+ *     - (optional) 'instance': when generated for a field, the field instance
+ *     - (optional) 'langcode': when generated for a field, the langcode
+ *       this field is being rendered in.
+ *     - (optional) 'delta': when generated for a field, the delta of the
+ *       currently handled address.
+ *     - (optional) 'entity': The entity that the form/display is created for
+ *     - (optional) 'entity_type': The entity_type of the entity provided
+ *     - (optional) 'entity_types': If an entity_type can't be established,
+ *       an array of types is passed instead.
+ *
+ */
+function hook_addressfield_handlers_alter(&$handlers, $address, $context) {}
diff --git a/addressfield.module b/addressfield.module
index 06affd4..96502a8 100644
--- a/addressfield.module
+++ b/addressfield.module
@@ -457,6 +457,21 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance,
       'langcode' => $langcode,
       'delta' => $delta,
     );
+
+    // Try to get the entity from the form_state
+    if (!empty($form_state['build_info']['args'][0])) {
+      $context['entity'] = $form_state['build_info']['args'][0];
+      // Add what info we know about the entity type
+      if (count($field['entity_types']) == 1) {
+        $context['entity_type'] = $field['entity_types'][0];
+      }
+      else {
+        $context['entity_types'] = $field['entity_types'];
+      }
+    }
+
+    // Allow other modules to alter the handlers used.
+    drupal_alter('addressfield_handlers', $settings['format_handlers'], $address, $context);
     $element += addressfield_generate($address, $settings['format_handlers'], $context);
 
     // Mark the form element as required if necessary.
@@ -624,7 +639,12 @@ function addressfield_field_formatter_view($entity_type, $entity, $field, $insta
           'instance' => $instance,
           'langcode' => $langcode,
           'delta' => $delta,
+          'entity_type' => $entity_type,
+          'entity' => $entity,
         );
+
+        // Allow other modules to alter the handlers used.
+        drupal_alter('addressfield_handlers', $handlers, $address, $context);
         $element[$delta] = addressfield_generate($address, $handlers, $context);
       }
       break;
-- 
1.7.5

