You are here

class UsersMapping in Replication 8.2

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

Hierarchy

Expanded class hierarchy of UsersMapping

2 files declare their use of UsersMapping
ContentEntityNormalizer.php in src/Normalizer/ContentEntityNormalizer.php
FileEntityNormalizer.php in src/Normalizer/FileEntityNormalizer.php
1 string reference to 'UsersMapping'
replication.services.yml in ./replication.services.yml
replication.services.yml
1 service uses UsersMapping
replication.users_mapping in ./replication.services.yml
Drupal\replication\UsersMapping

File

src/UsersMapping.php, line 9

Namespace

Drupal\replication
View source
class UsersMapping {

  /**
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;
  function __construct(ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
    $this->configFactory = $config_factory;
    $this->entityTypeManager = $entity_type_manager;
  }

  /**
   * Returns UID from replication.settings config object.
   *
   * @return int
   */
  public function getUidFromConfig() {
    return $this->configFactory
      ->get('replication.settings')
      ->get('uid');
  }

  /**
   * Maps user reference field.
   *
   * @param array $entity
   * @param string $field_name
   *
   * @return array
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
UsersMapping::$configFactory protected property
UsersMapping::$entityTypeManager protected property
UsersMapping::getUidFromConfig public function Returns UID from replication.settings config object.
UsersMapping::mapReferenceField public function Maps user reference field.
UsersMapping::__construct function