Add Drupal application
This commit is contained in:
parent
c00b769d67
commit
4bb39011f3
261 changed files with 22997 additions and 0 deletions
3
drupal/web/modules/custom/dtc_import/dtc_import.info.yml
Normal file
3
drupal/web/modules/custom/dtc_import/dtc_import.info.yml
Normal file
|
@ -0,0 +1,3 @@
|
|||
name: DTC Import
|
||||
core: 8.x
|
||||
type: module
|
|
@ -0,0 +1,9 @@
|
|||
services:
|
||||
Drupal\Core\Entity\EntityTypeManagerInterface:
|
||||
alias: entity_type.manager
|
||||
|
||||
Drupal\dtc_import\Service\Importer\CsvSessionImporter:
|
||||
autowire: true
|
||||
|
||||
Drupal\dtc_import\Service\Importer\CsvSpeakerImporter:
|
||||
autowire: true
|
6
drupal/web/modules/custom/dtc_import/sessions.csv
Normal file
6
drupal/web/modules/custom/dtc_import/sessions.csv
Normal file
|
@ -0,0 +1,6 @@
|
|||
Test Driven Drupal,Oliver Davies
|
||||
Introduction to Views,Tom Metcalfe
|
||||
Taking Flight with Tailwind CSS,Oliver Davies
|
||||
The Real State of Drupal,Dan McNamara
|
||||
Automate to manage repetitive tasks with Ansible,Daniel Pickering
|
||||
Doing good with Drupal,Matt Howarth
|
|
5
drupal/web/modules/custom/dtc_import/speakers.csv
Normal file
5
drupal/web/modules/custom/dtc_import/speakers.csv
Normal file
|
@ -0,0 +1,5 @@
|
|||
Oliver Davies
|
||||
Tom Metcalfe
|
||||
Dan McNamara
|
||||
Matt Howarth
|
||||
Daniel Pickering
|
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
|
||||
namespace Drupal\dtc_import\Service\Importer;
|
||||
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
abstract class CsvImporter implements ImporterInterface {
|
||||
|
||||
abstract protected function headings(): array;
|
||||
|
||||
protected function getCsv(string $path): string {
|
||||
return file_get_contents($path);
|
||||
}
|
||||
|
||||
protected function splitRows(string $rows): Collection {
|
||||
return collect(explode(PHP_EOL, $rows));
|
||||
}
|
||||
|
||||
protected function splitRow(string $row): Collection {
|
||||
return collect(explode(',', $row));
|
||||
}
|
||||
|
||||
protected function mapFieldsToHeadings(Collection $session): Collection {
|
||||
return $session->zip($this->headings())
|
||||
->mapWithKeys(function ($session): array {
|
||||
list($value, $key) = $session;
|
||||
return [$key => $value];
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\dtc_import\Service\Importer;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class CsvSessionImporter extends CsvImporter {
|
||||
|
||||
const NODE_TYPE_SESSION = 'session';
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
private $sessionStorage;
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
private $userStorage;
|
||||
|
||||
/**
|
||||
* CsvImporter constructor.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
|
||||
* The entity type manager.
|
||||
*
|
||||
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
|
||||
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
|
||||
*/
|
||||
public function __construct(
|
||||
EntityTypeManagerInterface $entityTypeManager
|
||||
) {
|
||||
$this->sessionStorage = $entityTypeManager->getStorage('node');
|
||||
$this->userStorage = $entityTypeManager->getStorage('user');
|
||||
}
|
||||
|
||||
public function import(): void {
|
||||
$csv = $this->getCsv(__DIR__ . '/../../../sessions.csv');
|
||||
|
||||
$this->splitRows($csv)->filter()->map(function (string $row): Collection {
|
||||
return $this->splitRow($row);
|
||||
})->map(function (Collection $session): Collection {
|
||||
return $this->mapFieldsToHeadings($session);
|
||||
})->map(function (Collection $values): array {
|
||||
return $this->findSessionNode($values);
|
||||
})->filter(function (array $sessionArray): bool {
|
||||
return !$sessionArray['node'];
|
||||
})->map(function (array $sessionItem) {
|
||||
list($values, $node) = $sessionItem;
|
||||
|
||||
return $this->createSessionNode($values);
|
||||
});
|
||||
}
|
||||
|
||||
protected function headings(): array {
|
||||
return ['title', 'field_speakers'];
|
||||
}
|
||||
|
||||
private function findSessionNode(Collection $values): array {
|
||||
$node = collect($this->sessionStorage->loadByProperties([
|
||||
'title' => $values->get('title'),
|
||||
'type' => self::NODE_TYPE_SESSION,
|
||||
]))->first();
|
||||
|
||||
return [$values, $node];
|
||||
}
|
||||
|
||||
private function createSessionNode(Collection $values): NodeInterface {
|
||||
$values = $values->merge([
|
||||
'field_speakers' => $this->findSpeakers($values->get('field_speakers')),
|
||||
'type' => self::NODE_TYPE_SESSION,
|
||||
]);
|
||||
|
||||
return tap($this->sessionStorage->create($values->toArray()), function (NodeInterface $session) {
|
||||
$session->setOwnerId(1);
|
||||
$session->setPublished();
|
||||
$session->save();
|
||||
});
|
||||
}
|
||||
|
||||
private function findSpeakers(string $speakers): array {
|
||||
return $this->userStorage->loadByProperties([
|
||||
'name' => explode(',', $speakers),
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\dtc_import\Service\Importer;
|
||||
|
||||
use Drupal\Core\Entity\EntityTypeManagerInterface;
|
||||
use Drupal\user\UserInterface;
|
||||
use Tightenco\Collect\Support\Collection;
|
||||
|
||||
class CsvSpeakerImporter extends CsvImporter {
|
||||
|
||||
/**
|
||||
* @var \Drupal\Core\Entity\EntityStorageInterface
|
||||
*/
|
||||
private $userStorage;
|
||||
|
||||
public function __construct(
|
||||
EntityTypeManagerInterface $entityTypeManager
|
||||
) {
|
||||
$this->userStorage = $entityTypeManager->getStorage('user');
|
||||
}
|
||||
|
||||
public function import(): void {
|
||||
$csv = $this->getCsv(__DIR__ . '/../../../speakers.csv');
|
||||
|
||||
$this->splitRows($csv)->filter()->map(function (string $row): Collection {
|
||||
return $this->splitRow($row);
|
||||
})->map(function (Collection $session): Collection {
|
||||
return $this->mapFieldsToHeadings($session);
|
||||
})->map(function (Collection $values): array {
|
||||
return $this->findUser($values);
|
||||
})->filter(function (array $speakerArray): bool {
|
||||
return !$speakerArray['user'];
|
||||
})->map(function (array $speakerArray): UserInterface {
|
||||
list($values, $user) = $speakerArray;
|
||||
|
||||
return $this->createUser($values);
|
||||
});
|
||||
}
|
||||
|
||||
protected function headings(): array {
|
||||
return ['name'];
|
||||
}
|
||||
|
||||
private function findUser(Collection $values) {
|
||||
$user = collect($this->userStorage->loadByProperties([
|
||||
'name' => $values->get('name'),
|
||||
]))->first();
|
||||
|
||||
return [$values, $user];
|
||||
}
|
||||
|
||||
private function createUser(Collection $values) {
|
||||
$values = $values->merge([
|
||||
'status' => TRUE,
|
||||
]);
|
||||
|
||||
return tap($this->userStorage->create($values->toArray()), function (UserInterface $user) {
|
||||
$user->save();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\dtc_import\Service\Importer;
|
||||
|
||||
interface ImporterInterface {
|
||||
|
||||
public function import(): void;
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue