Skip to content

Commit

Permalink
#29 Better integration
Browse files Browse the repository at this point in the history
  • Loading branch information
haringsrob committed Jan 22, 2017
1 parent 8f77d80 commit 4772f02
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 17 deletions.
5 changes: 5 additions & 0 deletions integration.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Integration
type: module
description: Integration Layer plugin.
core: 8.x
package: Integration
1 change: 1 addition & 0 deletions modules/integration_migrate/integration_migrate.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ core: 8.x
package: Integration

dependencies:
- integration
- migrate
- migrate_plus
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class IntegrationDocument extends EntityContentBase {
/**
* The document.
*
* @var array
* @var \Drupal\integration\Document\Document
*/
protected $document;

Expand All @@ -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());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -43,23 +43,28 @@ 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;
}

/**
* 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'];
}

/**
Expand All @@ -69,20 +74,36 @@ public function getDocument() {
* The type as string.
*/
public function getDocumentType() {
return $this->getDocument()['type'];
return $this->getDocument()->getMetadata('type');
}

/**
* {@inheritdoc}
*/
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);
}

Expand Down Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class MigrateDocumentEntityTest extends KernelTestBase {
'system',
'migrate',
'migrate_plus',
'integration',
'integration_migrate',
'language',
'node',
Expand Down Expand Up @@ -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);
}

}

0 comments on commit 4772f02

Please sign in to comment.