You are here

public function DemoUser::createContent in Open Social 10.2.x

Same name and namespace in other branches
  1. 8.9 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  2. 8 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  3. 8.2 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  4. 8.3 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  5. 8.4 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  6. 8.5 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  7. 8.6 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  8. 8.7 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  9. 8.8 modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  10. 10.3.x modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  11. 10.0.x modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()
  12. 10.1.x modules/custom/social_demo/src/DemoUser.php \Drupal\social_demo\DemoUser::createContent()

Creates content.

Return value

array An array with list of created entities.

Overrides DemoContentInterface::createContent

File

modules/custom/social_demo/src/DemoUser.php, line 72

Class

DemoUser
Class DemoUser.

Namespace

Drupal\social_demo

Code

public function createContent($generate = FALSE, $max = NULL) {
  $data = $this
    ->fetchData();
  if ($generate === TRUE) {
    $data = $this
      ->scrambleData($data, $max);
  }
  foreach ($data as $uuid => $item) {

    // Must have uuid and same key value.
    if ($uuid !== $item['uuid']) {
      drush_log(dt("User with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
      continue;
    }

    // Check whether user with same uuid already exists.
    $accounts = $this->entityStorage
      ->loadByProperties([
      'uuid' => $uuid,
    ]);
    if ($accounts) {
      drush_log(dt("User with uuid: {$uuid} already exists."), LogLevel::WARNING);
      continue;
    }

    // Load image by uuid and set to a profile.
    if (!empty($item['image'])) {
      $item['image'] = $this
        ->prepareImage($item['image'], $item['image_alt']);
    }
    else {

      // Set "null" to exclude errors during saving
      // (in cases when image will equal to "false").
      $item['image'] = NULL;
    }
    if (!empty($item['expertise'])) {
      $item['expertise'] = $this
        ->prepareTerms($item['expertise']);
    }
    if (!empty($item['interests'])) {
      $item['interests'] = $this
        ->prepareTerms($item['interests']);
    }
    if (!empty($item['roles'])) {
      $item['roles'] = array_filter($item['roles']);
    }
    if (empty($item['roles'])) {
      $item['roles'] = [
        AccountInterface::AUTHENTICATED_ROLE,
      ];
    }
    $entry = $this
      ->getEntry($item);
    $account = $this->entityStorage
      ->create($entry);
    $account
      ->setPassword($item['name']);
    $account
      ->enforceIsNew();
    $account
      ->save();
    if (!$account
      ->id()) {
      continue;
    }
    $this->content[$account
      ->id()] = $account;

    // Load the profile, since it's autocreated.
    $profiles = $this->profileStorage
      ->loadByProperties([
      'uid' => $account
        ->id(),
      'type' => ProfileType::load('profile')
        ->id(),
    ]);
    $profile = array_pop($profiles);
    if ($profile instanceof ProfileInterface) {
      $this
        ->fillProfile($profile, $item);
      $profile
        ->save();
    }
  }
  return $this->content;
}