Skip to content

Commit

Permalink
#29 Cleanup, phpcs, and multilingual support
Browse files Browse the repository at this point in the history
  • Loading branch information
haringsrob committed Jan 22, 2017
1 parent 4772f02 commit 917095e
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\integration_migrate\Plugin\migrate\destination;

use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
use Drupal\migrate\Row;
Expand Down Expand Up @@ -29,17 +30,30 @@ class IntegrationDocument extends EntityContentBase {
/**
* The source plugin.
*
* @var \Drupal\migrate\Plugin\MigrateSourceInterface
* @var \Drupal\integration_migrate\Plugin\migrate\source\IntegrationDocuments
*/
private $sourcePlugin;

/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager) {
$configuration['translations'] = TRUE;
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
$this->sourcePlugin = $this->migration->getSourcePlugin();
$this->document = $this->migration->getSourcePlugin()->getDocument();
}

/**
* Gets the document we are migrating.
*
* @return \Drupal\integration\Document\Document
* The document object.
*/
private function getDocument() {
if (empty($this->document)) {
$this->document = $this->sourcePlugin->getDocument();
}
return $this->document;
}

/**
Expand All @@ -49,7 +63,25 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
* Whether this destination is for translations.
*/
protected function isTranslationDestination() {
return !empty($this->document->getAvailableLanguages());
return !empty($this->getDocument()->getAvailableLanguages());
}

/**
* {@inheritdoc}
*
* @todo: This is a strange way of doing this.. need to invest more time.
*/
public function save(ContentEntityInterface $entity, array $old_destination_id_values = array()) {
// Go over the available languages.
foreach ($this->getDocument()->getAvailableLanguages() as $language) {
// The default language is already processed.
if ($language !== $this->getDocument()->getDefaultLanguage()) {
// Get the field values for the translation.
$entity->addTranslation($language, $this->getDocument()->setCurrentLanguage($language)->getCurrentLanguageFieldsValues());
}
}

return parent::save($entity, $old_destination_id_values);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ protected function getDocumentsArray() {
$document_raw = json_decode(file_get_contents($this->dataPath));
$document = new Document($document_raw);

// Add a row for each language.
$this->documentsArray[$document->getId()] = [
'id' => $document->getId(),
'language' => $document->getDefaultLanguage(),
'raw' => $document_raw,
'processed' => $document,
];
Expand Down Expand Up @@ -81,7 +83,7 @@ public function getDocumentType() {
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$language = 'en';
$language = $row->getSource()['language'];

foreach ($this->getDocument()->getFieldMachineNames() as $field_name) {
$row->setDestinationProperty($field_name, $this->getDocument()
Expand All @@ -96,6 +98,7 @@ public function prepareRow(Row $row) {
'changed' => 'changed',
'status' => 'status',
'sticky' => 'sticky',
'default_langcode' => 'default_langcode',
];

foreach ($static_metadata as $destination => $source) {
Expand All @@ -104,6 +107,15 @@ public function prepareRow(Row $row) {
->getMetadata($source));
}
}

// We need the language property.
$row->setDestinationProperty('language', $language);
$row->setDestinationProperty('langcode', $language);

$bar = $row->getIdMap();
$bar['destid2'] = $language;
$row->setIdMap($bar);

return parent::prepareRow($row);
}

Expand All @@ -118,7 +130,7 @@ public function __toString() {
* {@inheritdoc}
*/
public function getIds() {
// @todo: make dynamic
// @todo: make dynamic?
return [
'id' => [
'type' => 'integer',
Expand All @@ -143,7 +155,7 @@ protected function initializeIterator() {
* in field mappings, values are descriptions.
*/
public function fields() {
// TODO: Implement fields() method.
return parent::fields();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"_id": "10861",
"default_language": "en",
"languages": [
"en",
"fr"
],
"fields": {
"title": {
"en": [
"Test multilingual document title"
],
"fr": [
"Teste le titre du document multilingue"
]
}
},
"type": "integration_document_entity_test",
"created": "1235583913",
"changed": "1329926433",
"status": "0",
"sticky": "0"
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"fields": {
"title": {
"en": [
"Targeted Augmentation of Security Requirements in Somalia Vital to the Continuity of Relief Assistance"
"Test simple document title"
]
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Drupal\integration_migrate\tests\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\migrate\MigrateExecutable;
Expand All @@ -8,6 +10,9 @@
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;

/**
* Tests Migration of Documents using Integration.
*/
class MigrateDocumentEntityTest extends KernelTestBase {

/**
Expand All @@ -22,6 +27,7 @@ class MigrateDocumentEntityTest extends KernelTestBase {
'integration',
'integration_migrate',
'language',
'content_translation',
'node',
'field',
'user',
Expand All @@ -47,12 +53,17 @@ public function setUp() {
'type' => 'integration_document_entity_test',
'name' => 'Test node type',
])->save();

/** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
$content_translation_manager = \Drupal::service('content_translation.manager');

$content_translation_manager->setEnabled('node', 'integration_document_entity_test', TRUE);
}

/**
* Tests document import using migrate.
*/
public function testDocumentImport() {
public function testSimpleDocumentImport() {
$this->enableModules(['integration_migrate_entity']);

$definition = [
Expand All @@ -79,14 +90,65 @@ public function testDocumentImport() {
$this->assertNotNull($node);

// Check field data.
$this->assertEquals($node->getTitle(), "Targeted Augmentation of Security Requirements in Somalia Vital to the Continuity of Relief Assistance");
$this->assertEquals($node->getTitle(), "Test simple document title");

// 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);
}

/**
* Tests translated document import using migrate.
*
* It is ok to double test the properties already tested above, this way we
* ensure data is stable.
*/
public function testTranslatedDocumentImport() {
$this->enableModules(['integration_migrate_entity']);

$definition = [
'source' => [
'plugin' => 'integration_documents',
'data_path' => drupal_get_path('module', 'integration_migrate_entity') . '/data/101337.json',
],
'destination' => [
'plugin' => 'integration_document',
],
];

$migration = \Drupal::service('plugin.manager.migration')
->createStubMigration($definition);
$executable = new MigrateExecutable($migration, new MigrateMessage());

$result = $executable->import();
$this->assertEquals(MigrationInterface::RESULT_COMPLETED, $result);

/** @var \Drupal\node\NodeInterface $node */
$node = Node::load(10861);

// Check that we can load the node.
$this->assertNotNull($node);

// Check multilingual content.
$node_translated = $node->getTranslation('fr');
$this->assertNotEmpty($node_translated);

// Check en field data.
$this->assertEquals($node->getTitle(), "Test multilingual document title");

// Check fr field data.
$this->assertEquals($node_translated->getTitle(), 'Teste le titre du document multilingue');

// 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);

}

}
}
2 changes: 1 addition & 1 deletion src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public function getCurrentLanguageFieldsValues() {
foreach ($this->getFieldMachineNames() as $field_name) {
$result->{$field_name} = $this->getFieldValue($field_name);
}
$result->language = $this->getCurrentLanguage();
$result->langcode = $this->getCurrentLanguage();
return (array) $result;
}

Expand Down

0 comments on commit 917095e

Please sign in to comment.