DemoEntity.php in Open Social 8.3
File
modules/custom/social_demo/src/DemoEntity.php
View source
<?php
namespace Drupal\social_demo;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drush\Log\LogLevel;
abstract class DemoEntity extends DemoContent {
public function __construct(array $configuration, $plugin_id, $plugin_definition, DemoContentParserInterface $parser) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->parser = $parser;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('social_demo.yaml_parser'));
}
public function createContent() {
$data = $this
->fetchData();
$entity_type_id = $this->entityStorage
->getEntityTypeId();
foreach ($data as $uuid => $item) {
if ($uuid !== $item['uuid']) {
drush_log(dt("{$entity_type_id} with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
continue;
}
$entities = $this->entityStorage
->loadByProperties([
'uuid' => $uuid,
]);
if ($entities) {
drush_log(dt("{$entity_type_id} with uuid: {$uuid} already exists."), LogLevel::WARNING);
continue;
}
$entry = $this
->getEntry($item);
$entity = $this->entityStorage
->create($entry);
$entity
->save();
if ($entity
->id()) {
$this->content[$entity
->id()] = $entity;
}
}
}
protected function getEntry(array $item) {
$entry = [
'uuid' => $item['uuid'],
];
return $entry;
}
}