Created
October 2, 2012 16:29
-
-
Save anonymous/3820729 to your computer and use it in GitHub Desktop.
Drupal Relation Select insert() hook patch
This file contains hidden or 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
| <?php | |
| /** | |
| * Save an instance of a Relation Select field. | |
| * | |
| * Implements hook_field_insert(). | |
| */ | |
| function relation_select_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items){ | |
| $relation_type = relation_type_load($instance['settings']['relation_type']); | |
| list($entity_id) = entity_extract_ids($entity_type, $entity); | |
| // Load existing relations of this type | |
| $relations = relation_select_entity_get_relations($entity_type, $entity_id, $relation_type->relation_type); | |
| $rids = array(); | |
| foreach($relations as $relation){ | |
| $rids[] = $relation['relation_id']; | |
| } | |
| $relation_entities = relation_load_multiple($rids); | |
| foreach($items as $delta => $item) { | |
| if (!empty($item['endpoints'])) { | |
| $entity_keys = $item['endpoints']; | |
| if(isset($entity_keys[$langcode])) { | |
| $entity_keys = $entity_keys[$langcode]; | |
| } | |
| $entity_keys = relation_select_build_entity_keys($entity_type, $entity_id, $entity_keys); | |
| // Allow widgets to supply the relation id as rid or relation_id | |
| $rid = isset($item['rid']) ? $item['rid'] : (isset($item['relation_id']) ? $item['relation_id'] : NULL); | |
| // If there's a relation_id we're are editing an existing relation | |
| if($rid) { | |
| $relation = $relation_entities[$rid]; | |
| unset($relation_entities[$rid]); | |
| $relation->endpoints[$langcode] = $entity_keys; | |
| } | |
| else { | |
| $relation = relation_create($relation_type->relation_type, $entity_keys); | |
| } | |
| // Add the instance fields using Drupal Forms API magic | |
| $relation_instances = field_info_instances('relation', $relation->relation_type); | |
| if (isset($item['relation_fields'])) { | |
| foreach ($item['relation_fields'] as $relation_field_name => $relation_field) { | |
| if (isset($relation_instances[$relation_field_name])) { | |
| $relation_field_keys = array_keys($relation_field); | |
| $langcode = array_shift($relation_field_keys); | |
| $relation_field_items = array_shift($relation_field); | |
| $relation_field = field_info_field($relation_field_name); | |
| field_default_submit('relation', $relation, $relation_field, $relation_instances[$relation_field_name], $langcode, $relation_field_items, $item, $form_state); | |
| $relation->{$relation_field_name}[$langcode] = $relation_field_items; | |
| } | |
| else { | |
| $relation->{$relation_field_name} = $relation_field; | |
| } | |
| } | |
| } | |
| relation_save($relation); | |
| } | |
| } | |
| // Anything left in $relation_entities was not present in the submit and must be deleted | |
| foreach (array_keys($relation_entities) as $relation_id) { | |
| relation_delete($relation_id); | |
| } | |
| } | |
| function relation_select_build_entity_keys($entity_type, $entity_id, $endpoints){ | |
| $entity_keys = array( | |
| array( | |
| 'entity_type' => $entity_type, | |
| 'entity_id' => $entity_id, | |
| 'r_index' => 0 | |
| ) | |
| ); | |
| foreach($endpoints as $i => $value){ | |
| if (is_string($value)) { | |
| list($relation_entity_type, $relation_entity_id) = explode(':', $value); | |
| } | |
| else { | |
| $relation_entity_type = $value['entity_type']; | |
| $relation_entity_id = $value['entity_id']; | |
| } | |
| $entity_keys[] = array( | |
| 'entity_type' => $relation_entity_type, | |
| 'entity_id' => $relation_entity_id, | |
| 'r_index' => $i + 1 | |
| ); | |
| } | |
| return $entity_keys; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment