You are here

function cer_delete in Corresponding Entity References 7

$keys = array( 'home_entity_type' => $key[0], 'home_bundle' => $key[1], 'home_field' => $key[2], 'away_entity_type' => $key[3], 'away_bundle' => $key[4], 'away_field' => $key[5], );

File

./cer.crud.inc, line 250
Include file providing corresponding node reference insert, update, and delete handling.

Code

function cer_delete($home_entity, $keys, $process_unchanged = FALSE) {
  $types = array(
    'home' => $keys['home_entity_type'],
    'away' => $keys['away_entity_type'],
  );
  $ids = _cer_entity_ids($types);
  if (!isset($home_entity->{$keys['home_field']})) {
    return;
  }

  // Iterate through the field's references.
  foreach ($home_entity->{$keys}['home_field'] as $lang => $langdata) {
    foreach ($langdata as $reference) {
      if (!empty($reference['target_id'])) {

        // Load the referenced node if it is of the specified away type.
        if ($referenced_entity = entity_load($keys['away_entity_type'], array(
          $reference['target_id'],
        ), NULL, FALSE)) {
          $referenced_entity = $referenced_entity[$reference['target_id']];
          $referenced_entity->bundle_type = _cer_entity_get_bundle($referenced_entity, $keys['away_entity_type']);
          if ($referenced_entity->bundle_type == $keys['away_bundle']) {

            // Iterate through the away entity's references.
            foreach ($referenced_entity->{$keys['away_field']}[$lang] as $key => $value) {

              // Remove references to the deleted node.
              if ($value['target_id'] && $value['target_id'] == $home_entity->{$ids}['home']) {
                unset($referenced_entity->{$keys['away_field']}[$lang][$key]);
                _cer_update($keys['away_entity_type'], $referenced_entity);
                break;
              }
            }
          }
        }
      }
    }
  }
}