You are here

public function UsersMapping::mapReferenceField in Replication 8.2

Same name and namespace in other branches
  1. 8 src/UsersMapping.php \Drupal\replication\UsersMapping::mapReferenceField()

Maps user reference field.

Parameters

array $entity:

string $field_name:

Return value

array

File

src/UsersMapping.php, line 43

Class

UsersMapping

Namespace

Drupal\replication

Code

public function mapReferenceField($entity, $field_name) {
  $field_info = [];
  foreach ($entity[$field_name] as $delta => $item) {
    $users = [];
    if (isset($item['username'])) {
      $users = $this->entityTypeManager
        ->getStorage('user')
        ->loadByProperties([
        'name' => $item['username'],
      ]);
    }
    $user = reset($users);
    if ($user instanceof UserInterface && ($id = $user
      ->id())) {
      $field_info[$delta] = [
        'target_id' => $id,
      ];
    }
    else {
      $field_info[$delta] = [
        'target_id' => $this
          ->getUidFromConfig(),
      ];
    }
  }
  return $field_info;
}