You are here

function bibcite_entity_merge_entity in Bibliography & Citation 8

Same name and namespace in other branches
  1. 2.0.x modules/bibcite_entity/bibcite_entity.batch.inc \bibcite_entity_merge_entity()

Batch operation. Change change source contributor id to target.

Parameters

int $source_id: Source entity identifier.

int $target_id: Target entity identifier.

string $entity_type_id: Entity type identifier.

string $field_name: Field name to search.

array $context: Batch context array.

2 string references to 'bibcite_entity_merge_entity'
MergeConfirmForm::submitForm in modules/bibcite_entity/src/Form/MergeConfirmForm.php
Form submission handler.
MergeMultipleForm::submitForm in modules/bibcite_entity/src/Form/MergeMultipleForm.php
Form submission handler.

File

modules/bibcite_entity/bibcite_entity.batch.inc, line 25
Batch callbacks.

Code

function bibcite_entity_merge_entity($source_id, $target_id, $entity_type_id, $field_name, array &$context) {
  $entity_type_manager = \Drupal::entityTypeManager();
  $entity_type = $entity_type_manager
    ->getDefinition($entity_type_id);
  $reference_storage = $entity_type_manager
    ->getStorage('bibcite_reference');
  $storage = $entity_type_manager
    ->getStorage($entity_type_id);
  $source_entity = $storage
    ->load($source_id);
  if (empty($context['results'])) {
    $context['results'] = [
      'references' => [],
      'references_count' => 0,
      'entities' => [],
      'entity_type_id' => $entity_type_id,
      'label' => $source_entity
        ->label(),
    ];
  }
  if (empty($context['sandbox'])) {
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['current_id'] = 0;
    $context['sandbox']['max'] = $reference_storage
      ->getQuery()
      ->condition($field_name, $source_id)
      ->count()
      ->execute();
    $context['results']['references_count'] += $context['sandbox']['max'];
  }
  $query = $reference_storage
    ->getQuery();
  $query
    ->condition($field_name, $source_id)
    ->condition('id', $context['sandbox']['current_id'], '>')
    ->sort('id')
    ->range(0, 10);
  $reference_entities = $reference_storage
    ->loadMultiple($query
    ->execute());

  /** @var \Drupal\bibcite_entity\Entity\ReferenceInterface $reference_entity */
  foreach ($reference_entities as $reference_entity) {
    $field = $reference_entity
      ->get($field_name);
    $field_value = $field
      ->getValue();
    _bibcite_entity_process_field_value($field_value, $source_id, $target_id);
    $field
      ->setValue($field_value);
    $reference_entity
      ->save();
    $context['results']['references'][] = $reference_entity
      ->id() . ' : ' . $reference_entity
      ->label();
    $context['sandbox']['progress']++;
    $context['sandbox']['current_id'] = $reference_entity
      ->id();
  }
  $source_entity_message = t('Current @entity_type_label: @source_entity_label', [
    '@entity_type_label' => $entity_type
      ->getLabel(),
    '@source_entity_label' => $source_entity
      ->label(),
  ]);
  $context['message'] = $source_entity_message;
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  }
}