diff --git a/entityreference.module b/entityreference.module index 6b23390..e0bc51d 100644 --- a/entityreference.module +++ b/entityreference.module @@ -170,26 +170,18 @@ function _entityreference_get_behavior_handler($behavior) { /** * Get the selection handler for a given entityreference field. - * - * The handler contains most of the business logic of the field. */ -function entityreference_get_selection_handler($field, $instance = NULL) { - $object_cache = drupal_static(__FUNCTION__); - - if (!isset($object_cache[$field['field_name']])) { - ctools_include('plugins'); - $handler = $field['settings']['handler']; - $class = ctools_plugin_load_class('entityreference', 'selection', $handler, 'class'); +function entityreference_get_selection_handler($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { + ctools_include('plugins'); + $handler = $field['settings']['handler']; + $class = ctools_plugin_load_class('entityreference', 'selection', $handler, 'class'); - if (class_exists($class)) { - $object_cache[$field['field_name']] = call_user_func(array($class, 'getInstance'), $field, $instance); - } - else { - $object_cache[$field['field_name']] = EntityReference_SelectionHandler_Broken::getInstance($field, $instance); - } + if (class_exists($class)) { + return call_user_func(array($class, 'getInstance'), $field, $instance, $entity_type, $entity); + } + else { + return EntityReference_SelectionHandler_Broken::getInstance($field, $instance, $entity_type, $entity); } - - return $object_cache[$field['field_name']]; } /** @@ -217,7 +209,7 @@ function entityreference_field_validate($entity_type, $entity, $field, $instance return; } - $valid_ids = entityreference_get_selection_handler($field, $instance)->validateReferencableEntities(array_keys($ids)); + $valid_ids = entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->validateReferencableEntities(array_keys($ids)); $invalid_entities = array_diff_key($ids, array_flip($valid_ids)); if ($invalid_entities) { @@ -627,24 +619,29 @@ function entityreference_field_widget_settings_form($field, $instance) { /** * Implements hook_options_list(). */ -function entityreference_options_list($field, $instance = NULL) { - return entityreference_get_selection_handler($field, $instance)->getReferencableEntities(); +function entityreference_options_list($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { + return entityreference_get_selection_handler($field, $instance, $entity_type, $entity)->getReferencableEntities(); } /** * Implements hook_query_TAG_alter(). */ function entityreference_query_entityreference_alter(QueryAlterableInterface $query) { - $field = $query->getMetadata('field'); - $instance = $query->getMetadata('instance'); - entityreference_get_selection_handler($field, $instance)->entityFieldQueryAlter($query); + $handler = $query->getMetadata('entityreference_selection_handler'); + $handler->entityFieldQueryAlter($query); } /** * Implements hook_field_widget_form(). */ function entityreference_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) { - $handler = entityreference_get_selection_handler($field, $instance); + // Set some defaults for Drupal < 7.14. + // @todo: remove after 1.0. + $element += array( + '#entity_type' => NULL, + '#entity' => NULL, + ); + $handler = entityreference_get_selection_handler($field, $instance, $element['#entity_type'], $element['#entity']); if ($instance['widget']['type'] == 'entityreference_autocomplete' || $instance['widget']['type'] == 'entityreference_autocomplete_tags') { @@ -937,7 +934,7 @@ function entityreference_field_formatter_view($entity_type, $entity, $field, $in switch ($display['type']) { case 'entityreference_label': - $handler = entityreference_get_selection_handler($field, $instance); + $handler = entityreference_get_selection_handler($field, $instance, $entity_type, $entity); foreach ($items as $delta => $item) { $label = $handler->getLabel($item['entity']); diff --git a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php index 103f7aa..2161338 100644 --- a/plugins/selection/EntityReference_SelectionHandler_Generic.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Generic.class.php @@ -11,26 +11,28 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select /** * Implements EntityReferenceHandler::getInstance(). */ - public static function getInstance($field, $instance) { - $entity_type = $field['settings']['target_type']; + public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { + $target_entity_type = $field['settings']['target_type']; // Check if the entity type does exist and has a base table. - $entity_info = entity_get_info($entity_type); + $entity_info = entity_get_info($target_entity_type); if (empty($entity_info['base table'])) { return EntityReference_SelectionHandler_Broken::getInstance($field, $instance); } - if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $entity_type)) { - return new $class_name($field, $instance); + if (class_exists($class_name = 'EntityReference_SelectionHandler_Generic_' . $target_entity_type)) { + return new $class_name($field, $instance, $entity_type, $entity); } else { - return new EntityReference_SelectionHandler_Generic($field, $instance); + return new EntityReference_SelectionHandler_Generic($field, $instance, $entity_type, $entity); } } - protected function __construct($field, $instance) { + protected function __construct($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { $this->field = $field; $this->instance = $instance; + $this->entity_type = $entity_type; + $this->entity = $entity; } /** @@ -196,6 +198,7 @@ class EntityReference_SelectionHandler_Generic implements EntityReference_Select $query->addTag($this->field['settings']['target_type'] . '_access'); $query->addTag('entityreference'); $query->addMetaData('field', $this->field); + $query->addMetaData('entityreference_selection_handler', $this); // Add the sort option. if (!empty($this->field['settings']['handler_settings']['sort'])) { diff --git a/plugins/selection/abstract.inc b/plugins/selection/abstract.inc index 692aaf6..fb4a151 100644 --- a/plugins/selection/abstract.inc +++ b/plugins/selection/abstract.inc @@ -14,7 +14,7 @@ interface EntityReference_SelectionHandler { * A field datastructure. * @return EntityReferenceHandler */ - public static function getInstance($field, $instance); + public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL); /** * Return a list of referencable entities. @@ -58,8 +58,8 @@ interface EntityReference_SelectionHandler { * A null implementation of EntityReference_SelectionHandler. */ class EntityReference_SelectionHandler_Broken implements EntityReference_SelectionHandler { - public static function getInstance($field, $instance) { - return new EntityReference_SelectionHandler_Broken($field, $instance); + public static function getInstance($field, $instance = NULL, $entity_type = NULL, $entity = NULL) { + return new EntityReference_SelectionHandler_Broken($field, $instance, $entity_type, $entity); } protected function __construct($field, $instance) {