You are here

public function DemoFile::createContent in Open Social 8.9

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

Creates content.

Return value

array An array with list of created entities.

Overrides DemoContentInterface::createContent

File

modules/custom/social_demo/src/DemoFile.php, line 61

Class

DemoFile
Class DemoFile.

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

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

    // Copy file from module.
    $item['uri'] = file_unmanaged_copy($this->parser
      ->getPath($item['path'], $this
      ->getModule(), $this
      ->getProfile()), $item['uri'], FILE_EXISTS_REPLACE);
    $item['uid'] = NULL;
    $entry = $this
      ->getEntry($item);
    $entity = $this->entityStorage
      ->create($entry);
    $entity
      ->save();
    if (!$entity
      ->id()) {
      continue;
    }
    $this->content[$entity
      ->id()] = $entity;
    if (!empty($item['crops'])) {
      $this
        ->applyCrops($item, $entity);
    }
  }
  return $this->content;
}