View source
<?php
namespace Drupal\social_demo;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drush\Log\LogLevel;
abstract class DemoTaxonomyTerm 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($generate = FALSE, $max = NULL) {
$data = $this
->fetchData();
if ($generate === TRUE) {
$data = $this
->scrambleData($data, $max);
}
foreach ($data as $uuid => $item) {
if ($uuid !== $item['uuid']) {
drush_log(dt("Term with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
continue;
}
$terms = $this->entityStorage
->loadByProperties([
'uuid' => $uuid,
]);
if ($terms) {
drush_log(dt("Term 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;
}
}
return $this->content;
}
protected function getEntry(array $item) {
$entry = [
'uuid' => $item['uuid'],
'name' => $item['name'],
'vid' => $item['vid'],
'created' => \Drupal::time()
->getRequestTime(),
];
return $entry;
}
}