You are here

public function MergeContactsAction::executeMultiple in CRM Core 8

Same name and namespace in other branches
  1. 8.3 modules/crm_core_contact/src/Plugin/Action/MergeContactsAction.php \Drupal\crm_core_contact\Plugin\Action\MergeContactsAction::executeMultiple()
  2. 8.2 modules/crm_core_contact/src/Plugin/Action/MergeContactsAction.php \Drupal\crm_core_contact\Plugin\Action\MergeContactsAction::executeMultiple()

Executes the plugin for an array of objects.

Parameters

array $objects: An array of entities.

Overrides ActionBase::executeMultiple

1 call to MergeContactsAction::executeMultiple()
MergeContactsAction::execute in modules/crm_core_contact/src/Plugin/Action/MergeContactsAction.php
Executes the plugin.

File

modules/crm_core_contact/src/Plugin/Action/MergeContactsAction.php, line 142

Class

MergeContactsAction
Merges 2 or more contacts.

Namespace

Drupal\crm_core_contact\Plugin\Action

Code

public function executeMultiple(array $objects) {
  $primary_contact = reset($objects);
  foreach ($objects as $cid => $contact) {
    if ($contact
      ->id() == $this->configuration['data']['contact_id']) {
      $primary_contact = $contact;
      unset($objects[$cid]);
      break;
    }
  }
  unset($this->configuration['data']['contact_id']);
  $wrappers = [];
  foreach ($objects as $contact) {
    $wrappers[$contact
      ->id()] = $contact;
  }

  // Updating contact fields from other selected contacts.
  foreach ($this->configuration['data'] as $field_name => $contact_id) {
    if ($primary_contact
      ->id() != $contact_id) {
      $primary_contact
        ->set($field_name, $wrappers[key($contact_id)]
        ->get($field_name)
        ->getValue());
    }
  }
  $primary_contact
    ->save();
  foreach (array_keys($wrappers) as $contact_id) {

    // Creating path aliases for contacts that will be deleted.
    $this->pathAliasStorage
      ->save('/crm-core/contact/' . $primary_contact
      ->id(), '/crm-core/contact/' . $contact_id);
    if ($this->moduleHandler
      ->moduleExists('crm_core_activity')) {

      // Replacing participant in existing activities.
      $query = $this->entityQuery
        ->get('crm_core_activity');
      $activities = $query
        ->condition('activity_participants.target_id', $contact_id)
        ->condition('activity_participants.target_type', 'crm_core_contact')
        ->execute();
      if (is_array($activities)) {
        foreach (Activity::loadMultiple($activities) as $activity) {
          foreach ($activity->activity_participants as $delta => $participant) {
            if ($participant->target_id == $contact_id) {
              $activity
                ->get('activity_participants')[$delta]
                ->setValue($primary_contact);
            }
          }
          $activity
            ->save();
        }
      }
    }
    if ($this->moduleHandler
      ->moduleExists('relation')) {

      // Replacing existing relations for contacts been deleted with new ones.
      $query = $this->entityQuery
        ->get('relation');
      $relations = $query
        ->condition('endpoints.entity_type', 'crm_core_contact', '=')
        ->condition('endpoints.entity_id', $contact_id, '=')
        ->execute();
      foreach ($relations as $relation_info) {
        $endpoints = [
          [
            'entity_type' => 'crm_core_contact',
            'entity_id' => $primary_contact
              ->id(),
          ],
        ];
        $relation = Relation::load($relation_info);
        foreach ($relation->endpoints as $endpoint) {
          if ($endpoint->entity_id != $contact_id) {
            $endpoints[] = [
              'entity_type' => $endpoint->entity_type,
              'entity_id' => $endpoint->entity_id,
            ];
          }
        }
        $new_relation = Relation::create([
          'relation_type' => $relation->relation_type->target_id,
          'endpoints' => $endpoints,
        ]);
        $new_relation
          ->save();
      }
    }
  }
  $count = count($wrappers);
  $singular = '%contacts contact merged to %dest.';
  $plural = '%contacts contacts merged to %dest.';
  $contacts_label = array_map(function ($contact) {
    return $contact
      ->label();
  }, $wrappers);
  $message = $this->translationManager
    ->formatPlural($count, $singular, $plural, [
    '%contacts' => implode(', ', $contacts_label),
    '%dest' => $primary_contact
      ->label(),
  ]);
  $this->entityTypeManager
    ->getStorage('crm_core_contact')
    ->delete($wrappers);
  drupal_set_message($message);
}