You are here

public function DemoNode::createContent in Open Social 8

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

Creates content.

Return value

array An array with list of created entities.

Overrides DemoContentInterface::createContent

File

modules/custom/social_demo/src/DemoNode.php, line 63

Class

DemoNode
Class DemoNode.

Namespace

Drupal\social_demo

Code

public function createContent() {
  $data = $this
    ->fetchData();
  foreach ($data as $uuid => $item) {

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

    // Check whether node with same uuid already exists.
    $nodes = $this->entityStorage
      ->loadByProperties([
      'uuid' => $uuid,
    ]);
    if (reset($nodes)) {
      drush_log(dt("Node 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 node.
    $item['uid'] = $account
      ->id();
    if (isset($item['created'])) {
      $item['created'] = $this
        ->createDate($item['created']);
    }
    else {
      $item['created'] = \Drupal::time()
        ->getRequestTime();
    }
    $entry = $this
      ->getEntry($item);
    $entity = $this->entityStorage
      ->create($entry);
    $entity
      ->save();
    if ($entity
      ->id()) {
      $this->content[$entity
        ->id()] = $entity;
      if (!empty($item['group'])) {
        $this
          ->createGroupContent($entity, $item['group']);
      }
      if (isset($item['followed_by'])) {
        $this
          ->createFollow($entity, $item['followed_by']);
      }
    }
  }
  return $this->content;
}