You are here

public function Profile::populateFromProfile in Profile 8

Populates the profile with field values from the other profile.

Parameters

\Drupal\profile\Entity\ProfileInterface $profile: The other profile.

string[] $field_names: The names of fields to transfer. If empty, all configurable fields will be transferred.

Return value

$this

Overrides ProfileInterface::populateFromProfile

File

src/Entity/Profile.php, line 219

Class

Profile
Defines the profile entity class.

Namespace

Drupal\profile\Entity

Code

public function populateFromProfile(ProfileInterface $profile, array $field_names = []) {

  // Transfer all configurable fields by default.
  $field_names = $field_names ?: $this
    ->getConfigurableFieldNames($profile);
  $profile_values = $profile
    ->toArray();
  foreach ($field_names as $field_name) {
    if (isset($profile_values[$field_name]) && $this
      ->hasField($field_name)) {
      $this
        ->set($field_name, $profile_values[$field_name]);
    }
  }
  return $this;
}