View source
<?php
namespace Drupal\social_demo;
use Drupal\Core\Entity\Entity;
use Drupal\flag\Entity\Flagging;
use Drupal\user\UserStorageInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\node\NodeInterface;
use Drupal\group\Entity\GroupContent;
use Drush\Log\LogLevel;
abstract class DemoNode extends DemoContent {
protected $userStorage;
protected $groupStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DemoContentParserInterface $parser, UserStorageInterface $user_storage, EntityStorageInterface $group_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->parser = $parser;
$this->groupStorage = $group_storage;
$this->userStorage = $user_storage;
}
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'), $container
->get('entity.manager')
->getStorage('user'), $container
->get('entity.manager')
->getStorage('group'));
}
public function createContent() {
$data = $this
->fetchData();
foreach ($data as $uuid => $item) {
if ($uuid !== $item['uuid']) {
drush_log(dt("Node with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
continue;
}
$nodes = $this->entityStorage
->loadByProperties([
'uuid' => $uuid,
]);
if (reset($nodes)) {
drush_log(dt("Node with uuid: {$uuid} already exists."), LogLevel::WARNING);
continue;
}
$account = $this
->loadByUuid('user', $item['uid']);
if (!$account) {
drush_log(dt("Account with uuid: {$item['uid']} doesn't exists."), LogLevel::ERROR);
continue;
}
$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;
}
protected function createDate($date_string) {
if ($date_string === 'now') {
return time();
}
$timestamp = explode('|', $date_string);
$date = strtotime($timestamp[0]);
$date = date('Y-m-d', $date) . 'T' . $timestamp[1] . ':00';
return strtotime($date);
}
protected function getEntry(array $item) {
$entry = [
'uuid' => $item['uuid'],
'langcode' => $item['langcode'],
'created' => $item['created'],
'uid' => $item['uid'],
'title' => $item['title'],
'type' => $item['type'],
'body' => [
'value' => $this
->checkMentionOrLinkByUuid($item['body']),
'format' => 'basic_html',
],
];
return $entry;
}
public function createGroupContent(NodeInterface $entity, $uuid) {
$groups = $this->groupStorage
->loadByProperties([
'uuid' => $uuid,
]);
if ($groups) {
$group = current($groups);
$plugin_id = 'group_node:' . $entity
->bundle();
$plugin = $group
->getGroupType()
->getContentPlugin($plugin_id);
$group_content = GroupContent::create([
'type' => $plugin
->getContentTypeConfigId(),
'gid' => $group
->id(),
'entity_id' => $entity
->id(),
]);
$group_content
->save();
}
}
public function createFollow(Entity $entity, array $uuids) {
foreach ($uuids as $uuid) {
$account = $this
->loadByUuid('user', $uuid);
$properties = [
'uid' => $account
->id(),
'entity_type' => 'node',
'entity_id' => $entity
->id(),
'flag_id' => 'follow_content',
];
$flaggings = \Drupal::entityTypeManager()
->getStorage('flagging')
->loadByProperties($properties);
$flagging = reset($flaggings);
if (empty($flagging)) {
$flagging = Flagging::create($properties);
if ($flagging) {
$flagging
->save();
}
}
}
}
}