diff --git a/integration.info.yml b/integration.info.yml new file mode 100644 index 0000000..b0fa126 --- /dev/null +++ b/integration.info.yml @@ -0,0 +1,5 @@ +name: Integration +type: module +description: Integration Layer plugin. +core: 8.x +package: Integration diff --git a/modules/integration_migrate/integration_migrate.info.yml b/modules/integration_migrate/integration_migrate.info.yml index bc68877..95b2684 100644 --- a/modules/integration_migrate/integration_migrate.info.yml +++ b/modules/integration_migrate/integration_migrate.info.yml @@ -5,5 +5,6 @@ core: 8.x package: Integration dependencies: + - integration - migrate - migrate_plus diff --git a/modules/integration_migrate/src/Plugin/migrate/destination/IntegrationDocument.php b/modules/integration_migrate/src/Plugin/migrate/destination/IntegrationDocument.php index 8082922..3e52d8c 100644 --- a/modules/integration_migrate/src/Plugin/migrate/destination/IntegrationDocument.php +++ b/modules/integration_migrate/src/Plugin/migrate/destination/IntegrationDocument.php @@ -22,7 +22,7 @@ class IntegrationDocument extends EntityContentBase { /** * The document. * - * @var array + * @var \Drupal\integration\Document\Document */ protected $document; @@ -49,7 +49,7 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition * Whether this destination is for translations. */ protected function isTranslationDestination() { - return !empty($this->document['languages']); + return !empty($this->document->getAvailableLanguages()); } /** diff --git a/modules/integration_migrate/src/Plugin/migrate/source/IntegrationDocuments.php b/modules/integration_migrate/src/Plugin/migrate/source/IntegrationDocuments.php index 3ffb712..de2a6bd 100644 --- a/modules/integration_migrate/src/Plugin/migrate/source/IntegrationDocuments.php +++ b/modules/integration_migrate/src/Plugin/migrate/source/IntegrationDocuments.php @@ -2,10 +2,10 @@ namespace Drupal\integration_migrate\Plugin\migrate\source; +use Drupal\integration\Document\Document; use Drupal\migrate\Plugin\migrate\source\SourcePluginBase; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; -use Drupal\Tests\user\Kernel\TempStoreDatabaseTest; /** * Source plugin for retrieving data via URLs. @@ -43,10 +43,14 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition */ protected function getDocumentsArray() { if (empty($this->documentsArray)) { - $document = json_decode(file_get_contents($this->dataPath), TRUE); - $document['id'] = $document['_id']; - $data[$document['_id']] = $document; - $this->documentsArray = $data; + $document_raw = json_decode(file_get_contents($this->dataPath)); + $document = new Document($document_raw); + + $this->documentsArray[$document->getId()] = [ + 'id' => $document->getId(), + 'raw' => $document_raw, + 'processed' => $document, + ]; } return $this->documentsArray; } @@ -54,12 +58,13 @@ protected function getDocumentsArray() { /** * Gets the document. * - * @return array + * @return Document * The array of field data. */ public function getDocument() { $documents = $this->getDocumentsArray(); - return reset($documents); + $document = reset($documents); + return $document['processed']; } /** @@ -69,7 +74,7 @@ public function getDocument() { * The type as string. */ public function getDocumentType() { - return $this->getDocument()['type']; + return $this->getDocument()->getMetadata('type'); } /** @@ -77,12 +82,28 @@ public function getDocumentType() { */ public function prepareRow(Row $row) { $language = 'en'; - foreach ($row->getSourceProperty('fields') as $field_id => $field_data) { - $row->setDestinationProperty($field_id, reset($field_data[$language])); + + foreach ($this->getDocument()->getFieldMachineNames() as $field_name) { + $row->setDestinationProperty($field_name, $this->getDocument() + ->getFieldValue($field_name, $language)); + } + + // @todo: Static metadata, this can go into Document I think.. + $static_metadata = [ + 'nid' => '_id', + 'bundle' => 'type', + 'created' => 'created', + 'changed' => 'changed', + 'status' => 'status', + 'sticky' => 'sticky', + ]; + + foreach ($static_metadata as $destination => $source) { + if (!is_null($this->getDocument()->getMetadata($source))) { + $row->setDestinationProperty($destination, $this->getDocument() + ->getMetadata($source)); + } } - $row->setDestinationProperty('id', $row->getSourceProperty('id')); - $row->setDestinationProperty('nid', $row->getSourceProperty('id')); - $row->setSourceProperty('bundle', $row->getSourceProperty('type')); return parent::prepareRow($row); } @@ -122,7 +143,7 @@ protected function initializeIterator() { * in field mappings, values are descriptions. */ public function fields() { - $b = 'f'; // TODO: Implement fields() method. + return parent::fields(); } -} \ No newline at end of file +} diff --git a/modules/integration_migrate/tests/src/Kernel/MigrateDocumentEntityTest.php b/modules/integration_migrate/tests/src/Kernel/MigrateDocumentEntityTest.php index b5be189..cf3dc1b 100644 --- a/modules/integration_migrate/tests/src/Kernel/MigrateDocumentEntityTest.php +++ b/modules/integration_migrate/tests/src/Kernel/MigrateDocumentEntityTest.php @@ -19,6 +19,7 @@ class MigrateDocumentEntityTest extends KernelTestBase { 'system', 'migrate', 'migrate_plus', + 'integration', 'integration_migrate', 'language', 'node', @@ -74,7 +75,18 @@ public function testDocumentImport() { /** @var \Drupal\node\NodeInterface $node */ $node = Node::load(10861); + // Check that we can load the node. $this->assertNotNull($node); + + // Check field data. + $this->assertEquals($node->getTitle(), "Targeted Augmentation of Security Requirements in Somalia Vital to the Continuity of Relief Assistance"); + + // Check metadata. + $this->assertEquals($node->getType(), 'integration_document_entity_test'); + $this->assertEquals($node->getCreatedTime(), '1235583913'); + $this->assertEquals($node->getChangedTime(), '1329926433'); + $this->assertEquals($node->isPublished(), FALSE); + $this->assertEquals($node->isSticky(), FALSE); } } \ No newline at end of file