You are here

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

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

Creates content.

Return value

array An array with list of created entities.

Overrides DemoContentInterface::createContent

File

modules/custom/social_demo/src/DemoGroup.php, line 60

Class

DemoGroup
Class DemoGroup.

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("Group with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
      continue;
    }

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

    // Try to load a user account (author's account).
    $account = $this
      ->loadByUuid('user', $item['uid']);
    if (!$account) {
      drush_log(dt("Account with uuid: {$item['uid']} doesn't exists."), LogLevel::ERROR);
      continue;
    }

    // Create array with data of a group.
    $item['uid'] = $account
      ->id();
    $item['created'] = $item['changed'] = $this
      ->createDate($item['created']);

    // Load image by uuid and set to a group.
    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;
    }

    // Attach key documents.
    if (!empty($item['files'])) {
      $item['files'] = $this
        ->prepareFiles($item['files']);
    }
    else {

      // Set "null" to exclude errors during saving
      // (in cases when array with files will empty).
      $item['files'] = NULL;
    }
    $entry = $this
      ->getEntry($item);
    $entity = $this->entityStorage
      ->create($entry);
    $entity
      ->save();
    if (!$entity
      ->id()) {
      continue;
    }
    $this->content[$entity
      ->id()] = $entity;
    if (!empty($item['members'])) {
      $managers = !empty($item['managers']) ? $item['managers'] : [];
      $this
        ->addMembers($item['members'], $managers, $entity);
    }
  }
  return $this->content;
}