Skip to content
Snippets Groups Projects
Commit fb973975 authored by Will Long's avatar Will Long
Browse files

inital commit

parents
No related branches found
No related tags found
No related merge requests found
<?php
/**
* @file
* Drush integration for migrate_json_source.
*/
function migrate_json_source_drush_command() {
$commands['entity-json'] = [
'description' => 'Get JSON for an entity.',
'aliases' => ['ejs'],
'arguments' => [
'entity_type' => 'The entity type',
'ids' => 'The entity IDs',
],
'options' => [
'all' => 'Get all entities of the specified type.'
],
];
return $commands;
}
/**
* Drush callback for entity-json command.
*/
function drush_migrate_json_source_entity_json($entity_type = NULL, $ids = NULL) {
if (!$entity_type) {
return drush_set_error('', dt("Must specify entity type."));
}
try {
$storage = \Drupal::entityTypeManager()->getStorage($entity_type);
}
catch (\Exception $e) {
return drush_set_error('', dt("Invalid entity type."));
}
$all = drush_get_option('all', FALSE);
$ids = array_filter(explode(',', $ids));
if ($all) {
$output = [];
foreach ($storage->loadMultiple() as $entity) {
$output[$entity->id()] = $entity->toArray();
}
drush_print(json_encode($output, JSON_PRETTY_PRINT));
return;
}
if (!$all && empty($ids)) {
return drush_set_error('', dt("Must specify entity IDs or use --all option."));
}
// Single entity.
if (count($ids) == 1) {
$id = reset($ids);
$entity = $storage->load($id);
if (!$entity) {
$args = [
'@type' => $entity_type,
'@id' => $id,
];
return drush_set_error('', dt("Unable to load entity @type:@id", $args));
}
drush_print(json_encode($entity->toArray(), JSON_PRETTY_PRINT));
return;
}
// Multiple entities.
$output = [];
foreach ($storage->loadMultiple($ids) as $entity) {
$output[$entity->id()] = $entity->toArray();
}
drush_print(json_encode($output, JSON_PRETTY_PRINT));
}
type: module
name: Migrate JSON Source
description: 'A migration source for JSON encoded data files.'
package: Migration
core: 8.x
<?php
namespace Drupal\migrate_json_source;
use GlobIterator;
class JsonGlobIterator extends GlobIterator {
public function __construct($path) {
$path = rtrim($path, '/');
$path = $path . '/*.json';
parent::__construct($path);
}
/**
* {@inheritdoc}
*/
public function current() {
/** @var \SplFileInfo $current */
$current = parent::current();
$data = json_decode(file_get_contents($current->getPathname()), TRUE);
if (!is_array($data)) {
$data = [];
}
$data['id'] = $current->getBasename();
return $data;
}
}
<?php
namespace Drupal\migrate_json_source\Plugin\migrate\source;
use Drupal\Component\Plugin\ConfigurablePluginInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Row;
use Drupal\migrate_json_source\JsonGlobIterator;
use FilesystemIterator;
use GlobIterator;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
/**
* JSON file set source.
*
* @MigrateSource(
* id = "json_fileset",
* source_module = "migrate_json_source"
* )
*/
class JsonFilesetSource extends SourcePluginBase implements ConfigurablePluginInterface {
/**
* {@inheritdoc}
*/
public function fields() {
return [];
}
/**
* {@inheritdoc}
*/
public function __toString() {
return 'JSON encoded file set.';
}
/**
* {@inheritdoc}
*/
public function getIds() {
return [
'id' => [
'type' => 'string',
],
];
}
/**
* {@inheritdoc}
*/
protected function initializeIterator() {
return new JsonGlobIterator($this->getConfiguration()['path']);
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep(
$this->defaultConfiguration(),
$configuration
);
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [];
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
return [];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment