View source
<?php
namespace Drupal\social_demo;
use Drupal\user\UserStorageInterface;
use Drupal\file\FileStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\group\Entity\GroupInterface;
use Drush\Log\LogLevel;
abstract class DemoGroup extends DemoContent {
protected $userStorage;
protected $fileStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DemoContentParserInterface $parser, UserStorageInterface $user_storage, FileStorageInterface $file_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->parser = $parser;
$this->userStorage = $user_storage;
$this->fileStorage = $file_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('file'));
}
public function createContent() {
$data = $this
->fetchData();
foreach ($data as $uuid => $item) {
if ($uuid !== $item['uuid']) {
drush_log(dt("Group with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
continue;
}
$groups = $this->entityStorage
->loadByProperties([
'uuid' => $uuid,
]);
if ($groups) {
drush_log(dt("Group 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();
$item['created'] = $item['changed'] = $this
->createDate($item['created']);
if (!empty($item['image'])) {
$item['image'] = $this
->prepareImage($item['image'], $item['image_alt']);
}
else {
$item['image'] = NULL;
}
if (!empty($item['files'])) {
$item['files'] = $this
->prepareFiles($item['files']);
}
else {
$item['files'] = NULL;
}
$entry = $this
->getEntry($item);
$entity = $this->entityStorage
->create($entry);
$entity
->save();
if (!$entity
->id()) {
continue;
}
$this->content[$entity
->id()] = $entity;
if (!empty($item['members'])) {
$managers = !empty($item['managers']) ? $item['managers'] : [];
$this
->addMembers($item['members'], $managers, $entity);
}
}
return $this->content;
}
protected function getEntry(array $item) {
$entry = [
'uuid' => $item['uuid'],
'langcode' => $item['langcode'],
'type' => $item['type'],
'label' => $item['label'],
'field_group_description' => [
[
'value' => $item['description'],
'format' => 'basic_html',
],
],
'uid' => $item['uid'],
'created' => $item['created'],
'changed' => $item['changed'],
'field_group_image' => $item['image'],
'field_group_files' => $item['files'],
];
return $entry;
}
protected function createDate($date_string) {
$timestamp = explode('|', $date_string);
$date = strtotime($timestamp[0]);
$date = date('Y-m-d', $date) . 'T' . $timestamp[1] . ':00';
return strtotime($date);
}
protected function addMembers(array $members, array $managers, GroupInterface $entity) {
foreach ($members as $account_uuid) {
$account = $this->userStorage
->loadByProperties([
'uuid' => $account_uuid,
]);
if (($account = current($account)) && !$entity
->getMember($account)) {
$values = [];
if (in_array($account_uuid, $managers)) {
$values = [
'group_roles' => [
$entity
->bundle() . '-group_manager',
],
];
}
$entity
->addMember($account, $values);
}
}
}
protected function prepareFiles($files) {
$values = [];
foreach ($files as $file_uuid) {
$file = $this->fileStorage
->loadByProperties([
'uuid' => $file_uuid,
]);
if ($file) {
$values[] = [
'target_id' => current($file)
->id(),
];
}
}
return $values;
}
}