You are here

public function DemoComment::createContent in Open Social 8.8

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

Creates content.

Return value

array An array with list of created entities.

Overrides DemoContentInterface::createContent

File

modules/custom/social_demo/src/DemoComment.php, line 49

Class

DemoComment
Class DemoComment.

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

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

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

    // Create array with data of a comment.
    $item['uid'] = $account
      ->id();
    $item['pid'] = NULL;

    // Set parent comment if it is present.
    if (!empty($item['parent'])) {
      $comments = $this->entityStorage
        ->loadByProperties([
        'uuid' => $item['parent'],
      ]);
      if ($comments) {
        $comment = current($comments);
        $item['pid'] = $comment
          ->id();
      }
    }

    // Try and fetch the related entity.
    $entity = $this
      ->loadByUuid($item['entity_type'], $item['entity_id']);
    if (!$entity) {
      drush_log(dt("Entity {$item['entity_type']} with uuid: {$item['entity_id']} doesn't exists."), LogLevel::ERROR);
      continue;
    }
    if (!empty($item['created'])) {
      $item['created'] = $this
        ->createDate($item['created']);
      if ($item['created'] < $entity
        ->get('created')->value) {
        $item['created'] = \Drupal::time()
          ->getRequestTime();
      }
    }
    else {
      $item['created'] = \Drupal::time()
        ->getRequestTime();
    }
    $item['entity_id'] = $entity
      ->id();
    $entry = $this
      ->getEntry($item);
    $entity = $this->entityStorage
      ->create($entry);
    $entity
      ->save();
    if ($entity
      ->id()) {
      $this->content[$entity
        ->id()] = $entity;
    }
  }
  return $this->content;
}