You are here

function entityreference_usermerge_build_review_form_elements in User Merge 7.2

Implement hook_usermerge_build_review_form_elements().

File

includes/entityreference.usermerge.inc, line 11
Adds support for Entity Reference. Supplemental include loaded via usermerge_load_includes().

Code

function entityreference_usermerge_build_review_form_elements($review, $account_properties, $user_to_delete, $user_to_keep) {
  $entities = _entityreference_usermerge_get_referencing_entity_types();
  $entityreferences_user_to_delete = array();
  $entityreferences_user_to_keep = array();
  foreach ($entities as $entity_type => $fields) {
    foreach ($fields as $field) {
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', $entity_type)
        ->fieldCondition($field['field_name'], 'target_id', $user_to_delete->uid);
      $result = $query
        ->execute();
      if (count($result)) {
        if (isset($entityreferences_user_to_delete[$entity_type])) {
          $entityreferences_user_to_delete[$entity_type] = $entityreferences_user_to_delete[$entity_type] + $result[$entity_type];
        }
        else {
          $entityreferences_user_to_delete[$entity_type] = $result[$entity_type];
        }
      }
      $query = new EntityFieldQuery();
      $query
        ->entityCondition('entity_type', $entity_type)
        ->fieldCondition($field['field_name'], 'target_id', $user_to_keep->uid);
      $result = $query
        ->execute();
      if (count($result)) {
        if (isset($entityreferences_user_to_keep[$entity_type])) {
          $entityreferences_user_to_keep[$entity_type] = $entityreferences_user_to_keep[$entity_type] + $result[$entity_type];
        }
        else {
          $entityreferences_user_to_keep[$entity_type] = $result[$entity_type];
        }
      }
    }
  }
  if (count($entityreferences_user_to_delete) || count($entityreferences_user_to_keep)) {
    $review['entityreference'] = array(
      '#tree' => TRUE,
      '#theme' => 'usermerge_data_review_form_table',
      '#title' => t('Referencing entities (!module)', array(
        '!module' => t('Entity Reference'),
      )),
      '#attributes' => array(
        'no_merge',
        'property_label' => t('Entity'),
      ),
    );
    foreach ($entityreferences_user_to_delete as $entity_type => $entity_list) {
      $review['entityreference'][$entity_type]['property_name'] = array(
        '#type' => 'markup',
        '#markup' => $entity_type,
      );
      $review['entityreference'][$entity_type]['options']['user_to_delete'] = array(
        '#type' => 'markup',
        '#markup' => format_plural(count($entity_list), '1 %entity entity to be reassigned', '@count %entity entities to be reassigned', array(
          '%entity' => $entity_type,
        )),
      );
      $review['entityreference'][$entity_type]['fields'] = array(
        '#type' => 'hidden',
        '#value' => serialize($entities[$entity_type]),
      );
    }
    foreach ($entityreferences_user_to_keep as $entity_type => $entity_list) {
      $review['entityreference'][$entity_type]['property_name'] = array(
        '#type' => 'markup',
        '#markup' => $entity_type,
      );
      $review['entityreference'][$entity_type]['options']['user_to_keep'] = array(
        '#type' => 'markup',
        '#markup' => format_plural(count($entity_list), '1 %entity entity to be maintained', '@count %entity entities to be maintained', array(
          '%entity' => $entity_type,
        )),
      );
    }
    ksort($review['entityreference']);
    return $review;
  }
}