Created
June 30, 2015 12:10
-
-
Save gielfeldt/53f5b5c624d691b652c9 to your computer and use it in GitHub Desktop.
Add pseudo-delta support for relations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ting_reference.module b/ting_reference.module | |
index 29400f5..de5c722 100644 | |
--- a/ting_reference.module | |
+++ b/ting_reference.module | |
@@ -132,39 +132,21 @@ function ting_reference_field_update($entity_type, $entity, $field, $instance, $ | |
// Get existing relations. | |
$relations = ting_reference_get_relations($entity_type, $entity); | |
- // Book keeping. | |
- $keep = array(); | |
- $clear_cache = FALSE; | |
+ // Since there is no way to update the delta value of the endpoints, we need | |
+ // to delete all relations, and re-add them. | |
+ foreach ($relations as $rid => $relation) { | |
+ relation_delete($rid); | |
+ } | |
// Loop through items, create new and mark existing to be kept. | |
foreach ($items as $item) { | |
if (!empty($item['tid'])) { | |
- $found = FALSE; | |
- foreach ($relations as $relation) { | |
- if ($relation->endpoints['und'][1]['entity_id'] == $item['tid']) { | |
- $keep[$relation->rid] = $relation->rid; | |
- $found = TRUE; | |
- break; | |
- } | |
- } | |
- if (!$found) { | |
- ting_reference_create_relation($entity_type, $entity_id, $item['tid']); | |
- $clear_cache = TRUE; | |
- } | |
+ ting_reference_create_relation($entity_type, $entity_id, $item['tid']); | |
} | |
} | |
- // Clear cache if needed. | |
- if ($clear_cache) { | |
- field_cache_clear('field:' . $entity_type . ':' . $entity_id); | |
- } | |
- | |
- // Delete the relations that aren't present anymore. | |
- foreach ($relations as $rid => $relation) { | |
- if (!in_array($rid, $keep)) { | |
- relation_delete($rid); | |
- } | |
- } | |
+ // Clear cache. | |
+ field_cache_clear('field:' . $entity_type . ':' . $entity_id); | |
} | |
/** | |
@@ -447,6 +429,7 @@ function ting_reference_get_relations($entity_type, $entity) { | |
->condition('bundle', 'ting_reference') | |
->condition('endpoints_entity_type', $entity_type) | |
->condition('endpoints_entity_id', $entity_id) | |
+ ->orderBy('endpoints.entity_id') | |
->execute() | |
->fetchAllAssoc('entity_id'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment